 ## Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.

## Adobe Support Community

• Re: Help with custom calculation script - average ...

# Help with custom calculation script - average fields that are not NA unless all are NA

New Here ,
Mar 10, 2021 Mar 10, 2021

Copied

Hi,

I am rather new to Javascript and I'm currently working on a Custom Calculation Script for my form. I have the basic code to calculate the average of multiple fields and ignore fields that are NA. However, I would like the field to show NA if all the fields within the array have been selected as NA. Here is the code that I currently have:

var aFieldNames = new Array("Question 1.1", "Question 1.2", "Question 1.3", "Question 1.4", "Question 1.5", "Question 1.6");

// counter for non-N/A values;

var nCount = 0;

// variable to sum non-N/A values;

var nSum = 0;

// default value for result if no average computed;

event.value = 0;

// process array of field names;

for(i = 0; i < aFieldNames.length; i++) {

if(this.getField(aFieldNames[i]).valueAsString != "N/A") {

// field does not have a value of "N/A";

nCount++; // increment counter

nSum += Number(this.getField(aFieldNames[i]).value); // add value to sum

} // end value not N/A;

} // end loop processing one field;

// compute the average;

if(nCount != 0) {

// non-zero divisor so we can compute the average;

event.value = nSum / nCount;

}

Your help would be greatly appreciated as I am at a loss. Thank you! 🙂

Roxanne

TOPICS
JavaScript, PDF forms

Views

355

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more ## 1 Correct answer

Adobe Community Professional , Mar 11, 2021 Mar 11, 2021
Try this script:var nCount = 0; var nSum = 0; var na = 0; event.value = 0; for( var i=1; i<=6; i++){ if(this.getField("Question 1."+i).valueAsString == "N/A"){ na++; if(na == 6) event.value = "N/A";} else if(this.getField("Question 1."+i).valueAsString != "N/A" && this.getField("Question 1."+i).valueAsString != "" && this.getField("Question 1."+i).valueAsString != 0){ nCount++; nSum += Number(this.getField("Question 1."+i).value); event.value = nSum/nCount;}}

Likes 12 Replies 12
Mar 10, 2021 Mar 10, 2021

Copied

If you just compare if value is not N/A it will count blank fields in average so your calculation won't be correct,
add && if field is blank to comparison.
You can add another var to count if values is N/A and if that var is 6 set event.value to be N/A.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more New Here ,
Mar 11, 2021 Mar 11, 2021

Copied

Hi Nesa,

Thank you for your advice, it is greatly appreciated! I tried to make the modifications to the script as you described however it is not working (No syntax errors just doesn't calculate anything in preview). Could you please take a look at my script to see if I have made an error somewhere? Being brand new at this I have been struggling.

var aFieldNames = new Array("Question 1.1", "Question 1.2", "Question 1.3", "Question 1.4", "Question 1.5", "Question 1.6");
// counter for all N/A values;
var nCount = 6;
//default value for result if all N/A values;
event.value = N/A;
// counter for non-N/A values;
var nCount = 0;
// variable to sum non-N/A values;
var nSum = 0;
// default value for result if no average computed;
event.value = 0;
// process array of field names;
for(i = 0; i < aFieldNames.length; i++) {
if(this.getField(aFieldNames[i]).valueAsString != "N/A") {
// field does not have a value of "N/A";
nCount++; // increment counter
nSum += Number(this.getField(aFieldNames[i]).value); // add value to sum
} // end value not N/A;
} // end loop processing one field;
// compute the average;
if(nCount != 0) {
// non-zero divisor so we can compute the average;
event.value = nSum / nCount;
}

Thank you very much for your help 🙂

Roxanne

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more Mar 11, 2021 Mar 11, 2021

Copied

Try this script:

``````var nCount = 0;
var nSum = 0;
var na = 0;
event.value = 0;
for( var i=1; i<=6; i++){
if(this.getField("Question 1."+i).valueAsString == "N/A"){
na++;
if(na == 6)
event.value = "N/A";}
else if(this.getField("Question 1."+i).valueAsString != "N/A" && this.getField("Question 1."+i).valueAsString != "" && this.getField("Question 1."+i).valueAsString != 0){
nCount++;
nSum += Number(this.getField("Question 1."+i).value);
event.value = nSum/nCount;}}``````

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more New Here ,
Mar 12, 2021 Mar 12, 2021

Copied

Thank you so much for your help Nesa! That worked like a charm! One more question for you, one of my other fields is giving me the same issue. I had a simple calculation to multiply the previous field by 10 but now with the NA it doesn't work. Is there a simple script that i can add that would return NA but if it is a number to multiply it by 10?

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more Mar 12, 2021 Mar 12, 2021

Copied

Ok lets call "previous field" "Text1" so you want if Text1 is N/A event.value equal N/A else if Text1 is number event.value equal Text1*10?

Try this:

``````var f = this.getField("Text1");
if(f.valueAsString == "N/A")
event.value = "N/A";
else if(typeof f.value == "number")
event.value = f.value*10;
else event.value = "";``````

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more Mar 10, 2021 Mar 10, 2021

Copied

Replace

event.value = 0;

with

event.value = "N/A";

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more New Here ,
Mar 11, 2021 Mar 11, 2021

Copied

Hi Bernd,

Thank you for your reply! I had tried that before posting on here and it doesn't work. I don't receive a syntax error however the field just stops making any calculations at all if I modify the event value to anything but N/A.

Any other suggestions? I'm a newbie at this and troubleshooting is giving me hard time lol

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more Mar 11, 2021 Mar 11, 2021

Copied

What script does you use now?

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more New Here ,
Mar 11, 2021 Mar 11, 2021

Copied

Hi Bernd, I'm using this script :

var aFieldNames = new Array("Question 1.1", "Question 1.2", "Question 1.3", "Question 1.4", "Question 1.5", "Question 1.6");

// counter for non-N/A values;

var nCount = 0;

// variable to sum non-N/A values;

var nSum = 0;

// default value for result if no average computed;

event.value = 0;

// process array of field names;

for(i = 0; i < aFieldNames.length; i++) {

if(this.getField(aFieldNames[i]).valueAsString != "N/A") {

// field does not have a value of "N/A";

nCount++; // increment counter

nSum += Number(this.getField(aFieldNames[i]).value); // add value to sum

} // end value not N/A;

} // end loop processing one field;

// compute the average;

if(nCount != 0) {

// non-zero divisor so we can compute the average;

event.value = nSum / nCount;

}

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more Mar 11, 2021 Mar 11, 2021

Copied

Replace

event.value = 0;

with

event.value = "N/A";

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more New Here ,
Jun 04, 2021 Jun 04, 2021

Copied

I am having a simliar problem as the user above. I am attempting to make a form in adobe pro that utilized drop down boxes with three options: Yes, No or N/A. There will be approx. 60 drop down boxes: YNNA1, YNNA2, YNNA3... I need the form to calculate a total percentage of all Yes divded by all answers ommitting N/As. I used the code below updating with my forms information. But I am having no luck.  I am new to this whole world of scripts. Any help you have would be greatly appreciated!

var aFieldNames = new Array("YNNA1", "YNNA2", "YNNA3", "YNNA4", "YNNA5");

// counter for non-N/A values;

var nCount = 0;

// variable to sum non-N/A values;

var nSum = 0;

// default value for result if no average computed;

event.value = N/A;

// process array of field names;

for(i = 0; i < aFieldNames.length; i++) {

if(this.getField(aFieldNames[i]).valueAsString != "N/A") {

// field does not have a value of "N/A";

nCount++; // increment counter

nSum += Number(this.getField(aFieldNames[i]).value); // add value to sum

} // end value not N/A;

} // end loop processing one field;

// compute the average;

if(nCount != 0) {

// non-zero divisor so we can compute the average;

event.value = nSum / nCount;

}

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more Jun 04, 2021 Jun 04, 2021

Copied

LATEST

You want to divide "Yes" with total number of fields or only fields containing "N/A" including ones answered "Yes"?

Example: you have 10 fields, 5 has "Yes" 1 "No" and 4 is "N/A" so you divide 5/9 or 5/10 or 5/4?

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more  Resources