Post new topic Reply to topic  [ 5 posts ] 
Calculation Problem 
Author Message

Joined: Wed Nov 04, 2009 11:33 am
Posts: 3
Post Calculation Problem
Hello-

I have a web form to conduct a training exam.

Problem - My script (below) when run posts the sum of the the correct answer whether or not it was chosen

- It has 12 questions and the answers are displayed as radio buttons.
- Each field is labeled Question1, Question2......Question12
- The correct answer for each question has a value of "1" and the incorrect answer has a value of "0".
- At the conclusion of the exam, I have created a button that the user can click to grade the exam. The button script is as follows: <input type="button" value="Grade Exam" onclick="calculate();">

Problem - This script when run posts the sum in the correct field but it currently sums up the correct answers for each question regardless of whether or not they were the chosen answer.

- The calculate function is as follows:

<SCRIPT LANGUAGE="JavaScript">
function calculate()
{
var v_Question1 = document.getElementById('InsertRecordQuestion11');
var Question1 = parseFloat( v_Question1.value ,10 );

var v_Question2 = document.getElementById('InsertRecordQuestion21');
var Question2 = parseFloat( v_Question2.value ,10 );

var v_Question3 = document.getElementById('InsertRecordQuestion32');
var Question3 = parseFloat( v_Question3.value ,10 );

var v_Question4 = document.getElementById('InsertRecordQuestion44');
var Question4 = parseFloat( v_Question4.value ,10 );

var v_Question5 = document.getElementById('InsertRecordQuestion51');
var Question5 = parseFloat( v_Question5.value ,10 );

var v_Question6 = document.getElementById('InsertRecordQuestion60');
var Question6 = parseFloat( v_Question6.value ,10 );

var v_Question7 = document.getElementById('InsertRecordQuestion71');
var Question7 = parseFloat( v_Question7.value ,10 );

var v_Question8 = document.getElementById('InsertRecordQuestion81');
var Question8 = parseFloat( v_Question8.value ,10 );

var v_Question9 = document.getElementById('InsertRecordQuestion92');
var Question9 = parseFloat( v_Question9.value ,10 );

var v_Question10 = document.getElementById('InsertRecordQuestion100');
var Question10 = parseFloat( v_Question10.value ,10 );

var v_Question11 = document.getElementById('InsertRecordQuestion112');
var Question11 = parseFloat( v_Question11.value ,10 );

var v_Question12 = document.getElementById('InsertRecordQuestion122');
var Question12 = parseFloat(v_Question12.value ,10);

var sum = (Question1 + Question2 + Question3 + Question4 + Question5 + Question6 + Question7 + Question8 + Question9 + Question10 + Question11 + Question12);

document.getElementById('InsertRecordSum').value = Math.round(sum);

(document.getElementById('InsertRecordSum').value);
}
</script>


Any help would be greatly appreciated...


Wed Nov 04, 2009 12:23 pm
Profile
Frequent Poster

Joined: Mon Jul 20, 2009 12:40 pm
Posts: 32
Post Re: Calculation Problem
Hi,

The way to get the values off of radio buttons is something like the following:

Assuming InsertRecordquestion1 is a radio button -
for (i=0;i<document.form1.question1.length;i++)
{
if (document.form1.InsertRecordquestion1[i].checked)
{
question1_value = document.form1.InsertRecordquestion1[i].value;
}
}

If you could give me the URL of your form, I could look more into your script. Hope this helps.

Best,
-SO


Wed Nov 11, 2009 9:29 am
Profile

Joined: Wed Nov 04, 2009 11:33 am
Posts: 3
Post Re: Calculation Problem for radio button based questions
Thank you for your post.

I actually resolved it last night in a similar fashion and added a little logic to acknowledge whether the person passed or failed and render a status to determine whether or not they could print a completion certificate.

Hopefully this can help others as well.

The script is below and still includes my alerts to identify any problem spots:

<SCRIPT LANGUAGE="JavaScript">
function calculate()
{

var Question1 = 0;
if(document.getElementById('InsertRecordQuestion11').checked)
{
Question1 = 1;
}

var Question2 = 0;
if(document.getElementById('InsertRecordQuestion21').checked)
{
Question2 = 1;
}

var Question3 = 0;
if(document.getElementById('InsertRecordQuestion32').checked)
{
Question3 = 1;
}

var Question4 = 0;
if(document.getElementById('InsertRecordQuestion44').checked)
{
Question4 = 1;
}

var Question5 = 0;
if(document.getElementById('InsertRecordQuestion51').checked)
{
Question5 = 1;
}

var Question6 = 0;
if(document.getElementById('InsertRecordQuestion60').checked)
{
Question6 = 1;
}

var Question7 = 0;
if(document.getElementById('InsertRecordQuestion71').checked)
{
Question7 = 1;
}

var Question8 = 0;
if(document.getElementById('InsertRecordQuestion81').checked)
{
Question8 = 1;
}

var Question9 = 0;
if(document.getElementById('InsertRecordQuestion92').checked)
{
Question9 = 1;
}

var Question10 = 0;
if(document.getElementById('InsertRecordQuestion100').checked)
{
Question10 = 1;
}

var Question11 = 0;
if(document.getElementById('InsertRecordQuestion112').checked)
{
Question11 = 1;
}

var Question12 = 0;
if(document.getElementById('InsertRecordQuestion122').checked)
{
Question12 = 1;
}

alert("Before Sum");

var sum = (Question1 + Question2 + Question3 + Question4 + Question5 + Question6 + Question7 + Question8 + Question9 + Question10 + Question11 + Question12);

alert("After Sum");

document.getElementById('InsertRecordSum').value = Math.round(sum);

var total = 12;

alert("Before Result");

var result = (sum / total);

alert("After Result");

document.getElementById('InsertRecordResult').value = Math.round(result*100)/100;

(document.getElementById('InsertRecordSum').value);

alert("Before Status");

var status = "Failed";
if( result >= .85 )
{
status = "PASSED";
}

document.getElementById('InsertRecordStatus').value = (status);

alert("Afer Status");
}

document.getElementById("caspioform").onsubmit=calculate;
</script>

Best Regards.


Wed Nov 11, 2009 9:43 am
Profile
Frequent Poster

Joined: Mon Jul 20, 2009 12:40 pm
Posts: 32
Post Re: Calculation Problem
Hi rowimo,

Great! Good job! And thanks for posting the solution :)

Best,
-SO


Wed Nov 11, 2009 10:32 am
Profile

Joined: Mon Nov 16, 2009 11:28 pm
Posts: 17
Post Re: Calculation Problem
1. The problem statement, all variables and given/known data
I need to calculate the surface tension of jelly (gelatine dessert) or jell-o, as it is known in the states.

My textbook defines surface tension as Y=F/L. Is this the equation I would use to calculate surface tension? If so, how?


Mon Nov 16, 2009 11:32 pm
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © phpBB Group.