Answered
I am using Acrobat 8 AcroForms and wish to set a quiz with 4 multi choice radio buttons and then a text field at the end that calculates the correct answers as a percentage.
There are 50 questions so I am putting the correct answer as 'Export value = 2' and 3 incorrect answers 'Export values = 0."
The radio buttons are grouped Q1, Q2, Q3 etc.
Can you please help me with the javascript I need to put in the calculate field for the final text box?
var num_questions = 50;
var sum = 0;
// Loop through the radio button fields
for (var i = 1; i < num_questions + 1; i++) {
// Add current field value to sum
sum += getField("Q" + i).value;
}
// The variable "sum" now has the percent correct
// if all fields have been selected.
// If this is for a calculated field, you'd then do:
event.value = sum;
However, if not all fields are selected, what do you want to do? Perhaps something like:
var num_questions = 50;
var sum = 0;
var answered = 0;
// Loop through the radio button fields
for (var i = 1; i < num_questions + 1; i++) {// Get the current field value
var val = getField("Q" + i).value;
// If field is selected, count it
if (val != "Off") {
sum += val;
answered++;
}
}
// If all questions have been answered, show percentage
if (answered == num_questions) {
event.value = sum;
} else {
// Otherwise, indicate test is not complete
event.value = "Not complete!"
}
Note that it would be easy for a user to cheat with it set up this way if the result field is visible, but this should get you started.
George