Skip to main content
Inspiring
September 30, 2025
Question

JavaScript Calculate Average + Bonus Rating

  • September 30, 2025
  • 1 reply
  • 136 views

I have an evaluation sheet with JavaScript that caluclates the average score from Radio Button fields. Questions have a score of 1 through 5 (and N/A--which doesn't get calculated into the average so it doesn't impact their score). I have an additional Radio Button field titled "Bonus Rating." Currently, it is not included in the var fields/part of the user's average score.

 

What I need is "Bonus Rating" to be added to the numerator of the average, but not the denominator (so to speak) so it serves as a bonus benefit for their rating. I got the JavaScript from a different thread and everything works great! It's just this extra bonus rating that I'm not sure where I would add that in since I'm not well versed in Java 🙁Can I add a line of code somewhere to easily add Bonus Rating in?

 

I've attached a screenshot of the JavaScript I'm using. Thanks to anyone who can help!

1 reply

Thom Parker
Community Expert
Community Expert
September 30, 2025

You need to be more clear about how you want the math to work, but the bonus value is aquired in exactly the same way as the other values.  Add this code immediately after the loop.

 

// Note that this code assumes the export values of the bonus are a number, if not then some fixup code is necessary.

var nBonus = this.getField("Bonus Rating").valueAsString;

nBonus = (nBonus == "Off")?0:Number(nBonus);

 

How it's added depends on what you want. 

Right now the code is

     Result =  total/n;

What do you want? 

     Result = (total+ nBonus)/n;

   or

     Result = total/n + nBonus;

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
justinky_Author
Inspiring
October 1, 2025

Thank you for this, Thom. This worked like a charm!