Skip to main content
Participant
January 15, 2025
Question

Calculating a total percentage score based off radio button values

  • January 15, 2025
  • 2 replies
  • 276 views

Hello,

 

I feel like this should be relatively straight-forward, but I cannot seem to locate an answer - or, at least, I cannot seem to get any of the answers I've found to work for me.

 

I am creating a form (specifically a rubric) that has multiple groups of 6 radio buttons, each with values ranging from 5 to 0. I'm trying to figure out how to calculate the total percentage score for all the groups of radio buttons.

 

For instance, my two groups (Group1 and Group2) all have the radio button with a value of 5 selected, the score of the rubric would be 100%. Both groups with the value 4 selected would give me 80%, and so on.

 

Any help would be greatly appreciated! Thank you!

2 replies

Nesa Nurani
Community Expert
Community Expert
January 15, 2025

Format field as percentage and you can use this:

var total = 0;
var count = 0;
for(var i=1; i<=2; i++){
 var f = this.getField("Group"+i).valueAsString;
  if(f !== "Off"){
   total += Number(f);
   count++;}}
if(count == 0)
 event.value = 0;
else
 event.value = total/(count*5);

If you have more groups, change 2 in the loop to the number of groups.

BarlaeDC
Community Expert
Community Expert
January 15, 2025

Hi,

 

To be clear,

 

Group 1.   Group2.  Percentage

5                5              100

5                4              90

4                4              80

 

as so on?

 

if that is the case then the attached file shows how this can be done

 

there is a document javascript called updatePercentage which does all the work

 

function updatePercentage () {
// change these to the names of the two groups
var groupOne = this.getField("Group1");
var groupTwo = this.getField("Group2");

var groupOneUseValue = 0;
var groupTwoUseValue = 0;

//update the options so they match what you have
if ( groupOne.value == "Choice1") {
  groupOneUseValue = 5;
} else if ( groupOne.value == "Choice2"){
  groupOneUseValue = 4;
} else if ( groupOne.value == "Choice3"){
  groupOneUseValue = 3;
} else if ( groupOne.value == "Choice4"){
  groupOneUseValue = 2;
} else {
  groupOneUseValue = 1;
}

if ( groupTwo.value == "Choice1") {
  groupTwoUseValue = 5;
} else if ( groupTwo.value == "Choice2"){
  groupTwoUseValue = 4;
} else if ( groupTwo.value == "Choice3"){
  groupTwoUseValue = 3;
} else if ( groupTwo.value == "Choice4"){
  groupTwoUseValue = 2;
} else {
  groupTwoUseValue = 1;
}

// update this to the field you want to output too.
this.getField("Text2").value = (  groupOneUseValue
 + groupTwoUseValue ) * 10;
}