Skip to main content
Participant
July 31, 2022
Question

Need to create conditional calculation script that will add the results if greater than or equal 3

  • July 31, 2022
  • 1 reply
  • 745 views

I am creating a form where someone will respond to 75 statements (with each being a different field) on a likert scale of 0 to 4.  I can figure out how to create a calculation of the sum of all the fields but I also need to create a conditional calculation script that will look at the value for each statement and if the value is greater than or equal to 3 it adds the value into the total.  If it is 2 or less, then it can be left out of the calculation.

 

so it might look like:

 

Question1 = 3

Question2 = 2

Question3 = 4

Question4 = 1

Question5 = 3

Question6 = 3

Question7 = 2

Question8 = 4

Question9 = 3

Question10 = 2

 

So ideally, the script would be able to recognize that Question2, Question3, Question5, Question6, Question8, and Question9 would be added together into a total.  so 3+4+3+3+4+3 = 20

 

Any help would be greatly appreciated

 

thanks

This topic has been closed for replies.

1 reply

Nesa Nurani
Community Expert
Community Expert
July 31, 2022

If 'Question1-10' are field names you can do it like this:

var total = 0;
for( var i=1; i<=10; i++){
if(this.getField("Question"+i).valueAsString != "" && Number(this.getField("Question"+i).value) >= 3)
total += Number(this.getField("Question"+i).value);}
event.value = total;

 

If you have 75 'Question' fields replace 10 with 75.

Rtyler99Author
Participant
July 31, 2022

This is so great thank you!   One additional favor.  Same scenario but instead of adding all of them together, I just want to create a count of how many of the 75 questions have scores equal to or greater than 3.

 

thank you again,

 

Nesa Nurani
Community Expert
Community Expert
July 31, 2022

Just change this line:

total += Number(this.getField("Question"+i).value);

to:

total++;