These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Calculate radio buttons

Cracknell
Acrobat 9
Registered: Sep 8 2008
Posts: 29
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?

My Product Information:
Acrobat Pro 8.0, Macintosh
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
You made it easy by choosing a good field naming scheme and good export values. The following code intended as the custom calculation script for the result text field will calculate the percentage, assuming all radio button fields have a selection:

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
Cracknell
Acrobat 9
Registered: Sep 8 2008
Posts: 29
Thanks George, that's perfect!
Cracknell
Acrobat 9
Registered: Sep 8 2008
Posts: 29
Can I limit the decimal places the percent shows? At the moment its going screwy at question 8 and adding loads of decimal places. I now have 61 questions each with an correct answer export value of 1.64.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Sure. To limit it to one digit to the right of the decimal point, do this:

event.value = util.printf("%.1f", sum);


Change the 1 in the format string above if you want a different number of digits to the right of the decimal point.

George
Cracknell
Acrobat 9
Registered: Sep 8 2008
Posts: 29
Seem to have trouble getting the final result to show 100%. Could you help me with a java script to add that says if all answers correct display 'Congratulations 100%'

Also I'd like to set a pass mark, say 75% so if they get under it says, for example 'Failed 65%' and if over it says 'Passed 78%,
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Can you show us what you're currently using?

George
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You will need to make the result field with the "None" format and use JavaScript to build the formatted results. The average has been truncated to 3 decimal places to prevent rounding of a score of 7.495 from being rounded to a passing score.

And then change the sample code for the score calculation after gathering the totals to read:

The following code can be used in the result field's "Custom calculation script:" box. The result field has its format set to "None":

var num_questions = 50; number of questions on test
var num_answered = 0; // number of questions answered value not equal to "Off"
var num_correct = 0; // number of questions with a value of "2";
var fQuestion; // computed question field value
var sResult = 'Incomplete';
var fAverage = 0; // result of average number of correct scores
var sPercentage = '0.0%' // formatted percentage


for (i = 0; i < num_questions; i ++) {
fQuestion = this.getField('Q' + (i + 1) ).value; // compute question field name
if (fQuestion != "Off") { // an answered question
num_answered++; // increment answered count
if(fQuestion == 2) num_correct++; // correctly answered so increment correct count
} // end if answered
} // end question loop

// if all questions answered then process scores
if(num_answered == num_questions) {
fAverage = num_correct / num_questions; // correct count / number of questions
// truncate average to 3 decimal places so rounding will not make a passing score
fAverage = Math.floor(fAverage * 1000) / 1000; // for 3 decimal places
// format average score to a percentage string
sPercentage = util.printf('% 0.1f', (fAverage * 100) ) + '%';
// evaluate score
switch(true) {
case (fAverage == 1) : // 100.0%
sResult = 'Congratulations ' + sPercentage;
break;
case (fAverage < 0.75) :// less than 75.0%
sResult = 'Failed ' + sPercentage;
break;
defalut: // all other score are a pass
sResult = "Passed " + sPrecentage;
break;
} // end switch on score value
} // end if all answered

// display result
event.value = sResult;

George Kaiser

Cracknell
Acrobat 9
Registered: Sep 8 2008
Posts: 29
This is what I currently have - I tried to copy paste the new script supplied by gkai but there was an error at line two which said ; missing before statement. My export value for each correct radio button is 1.33.

var num_questions = 75;
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; event.value = util.printf("%.1f", sum);
} else {
// Otherwise, indicate test is not complete
event.value = "Not complete!"
}
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Sorry, there was a missing comnent marker

The code should be:

// enter number of questions on exam
var num_questions = 50; // number of questions on test

// script controlled values
var num_answered = 0; // number of questions answered value not equal to "Off"
var num_correct = 0; // number of questions with a value of "2";
var fQuestion; // computed question field value
var sResult = 'Incomplete';
var fAverage = 0; // result of average number of correct scores
var sPercentage = '0.0%' // formatted percentage

// process scores if all questions are answered
for (i = 0; i < num_questions; i ++) {
fQuestion = this.getField('Q' + (i + 1) ).value; // compute question field name
if (fQuestion != "Off") { // an answered question
num_answered++; // increment answered count
if(fQuestion == 2) num_correct++; // correctly answered so increment correct count
} // end if answered
} // end question loop

// if all questions answered then process scores
if(num_answered == num_questions) {
fAverage = num_correct / num_questions; // correct count / number of questions
// truncate average to 3 decimal places so rounding will not make a passing score
fAverage = Math.floor(fAverage * 1000) / 1000; // for 3 decimal places
// format average score to a percentage string
sPercentage = util.printf('% 0.1f', (fAverage * 100) ) + '%';

// evaluate score
switch(true) {
case (fAverage == 1) : // 100.0%
sResult = 'Congratulations ' + sPercentage;
break;
case (fAverage < 0.75) :// less than 75.0%
sResult = 'Failed ' + sPercentage;
break;
default: // all other score are a pass
sResult = "Passed " + sPercentage;
break;
} // end switch on score value
} // end if all answered

// display result
event.value = sResult;

George Kaiser

Cracknell
Acrobat 9
Registered: Sep 8 2008
Posts: 29
Thanks that perfect now.
Ahmed Hadi
Registered: Jun 12 2011
Posts: 18
Hello gentlemen,,

i'm a new member here, and i've a question about (Radio Buttons)

i've an Employees appraisal, and i've putted 6 of radio buttons in front if appraisal subjects like following:

(1)Attendance punctuality: 5, 4, 3, 2, 1, NA.

(2)Problem Solving: 5, 4, 3, 2, 1, NA.

(3)....
(4)....
(5)....
Until (25)


NOTE:
(,) = Radio buttons!


So! how can i calculate the whole radio buttons as points in on field and then i'll x points on 100 ÷ the numbers of questions x 5 to get a percentage result in another field for exaple:

field one= total points
field two= total points x 100 ÷ numbers of subjects x 5
field Three= percentage result.


please try to help me as soon as possible because i've to send the evaluation to the employees after two days :(


Thanks & Best Regards,,

Sincerely yours,,,

Ahmed Hadi
Executive Admin in IT Company.

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
How are you gathering the evaluation of each subject?

If it is one form per subject you will need to get each form's data into an Excel spreadsheet with one row for each subject for analysis.

You might want to look at Merge Data Files into Spreadsheet.




George Kaiser

Ahmed Hadi
Registered: Jun 12 2011
Posts: 18
Hello Mr. George

Thank you very much for your quick response, but i want to inform you that it's all in one form.


Sincerely yours,,

Sincerely yours,,,

Ahmed Hadi
Executive Admin in IT Company.