Skip to main content
December 11, 2025
Question

Calculation Issue in Forms

  • December 11, 2025
  • 2 replies
  • 462 views

I've created a form with several areas where calculations are carried out. There are four sections, each containing different versions of the "Score" field, and these scores are averaged in a corresponding "Area Score" field located below each section.  


The "Area Score" fields are calculating just fine with the following calculation:
var totalSum = 0;
var validFieldCount = 0;
var fieldNames = ["Score", "Score_2", "Score_3"];

for (var i = 0; i < fieldNames.length; i++) {
var field = this.getField(fieldNames[i]);

if (field != null) {
var fieldValue = field.valueAsString;

if (fieldValue !== "" && !isNaN(Number(fieldValue))) {
totalSum += Number(fieldValue);
validFieldCount++;
}
}
}

if (validFieldCount > 0) {
event.value = totalSum / validFieldCount /4;
} else {
event.value = ""; // Or 0, or "N/A", depending on desired behavior for no valid fields
}

 

I have a final section called "Evaluation Score," which is meant to be the average of all four "Area Scores." Initially, I used the built-in formula, "Value is the average of the following fields: Area Score, Area Score_2, Area Score_3, Area Score_4," but encountered issues because some entries use "N/A" instead of actual numbers. I tried several calculation methods for this section, but none worked correctly. What I need is a calculation that averages all four "Area Score" fields while ignoring any non-numeric values, since the standard formula doesn't handle non-numbers.  

2 replies

PDF Automation Station
Community Expert
Community Expert
December 11, 2025

When you say the non-numeric values are ignored, in your last sentence, do you mean they are taken as zero, or do you mean they are excluded from the average calculaton, lowering the number of fields that are averaged from 4?

PDF Automation Station
Community Expert
Community Expert
December 11, 2025

If  they are taken as zero use this formula:

var flds=["Area Score","Area Score_2","Area Score_3","Area Score_4"];
var ttl=0;
for(var i=0;i<flds.length;i++)
{
if(!isNaN(this.getField(flds[i]).value))
{ttl+=Number(this.getField(flds[i]).value)}
}
event.value=ttl/4;

If they are excluded from the average use this:

var flds=["Area Score","Area Score_2","Area Score_3","Area Score_4"];
var ttl=0;
var numFlds=0;
for(var i=0;i<flds.length;i++)
{
if(!isNaN(this.getField(flds[i]).value))
{ttl+=Number(this.getField(flds[i]).value);numFlds++;}

}
if(numFlds>0)
{event.value=ttl/numFields};

 

crs09sAuthor
December 12, 2025

Thank you so much! I believe the first formula works! 

Basically if there is an N/A we are going to exclude it from the score average as some questions on the scoring guidelines won't pertain to every evaulation. 

Thom Parker
Community Expert
Community Expert
December 11, 2025

the problem is this line:

 

if (fieldValue !== "" && !isNaN(Number(fieldValue))) 

 

Specifically ->  isNaN(Number(fieldValue))

 

The "isNaN()" function will always return false because "fieldValue" is being converted into a number before being passed into the isNaN() function. 

To fix the issue, change the line to:

 

if (fieldValue !== "" && !isNaN(fieldValue)) 

 

Now the average of totals will work with the same script used for the other averages.

And change the other scripts as well. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often