# Calculation Problem

## Recommended Posts

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:

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:

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);

}

Any help would be greatly appreciated...

##### Share on other sites

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

{

if (document.form1.InsertRecordquestion1.checked)

{

question1_value = document.form1.InsertRecordquestion1.value;

}

}

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

Best,

-SO

##### Share on other sites

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:

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;

}

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

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

var total = 12;

var result = (sum / total);

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

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

var status = "Failed";

if( result >= .85 )

{

status = "PASSED";

}

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

}

document.getElementById("caspioform").onsubmit=calculate;

Best Regards.

##### Share on other sites

Hi rowimo,

Great! Good job! And thanks for posting the solution Best,

-SO

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.