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

New Here ,
Mar 10, 2021 Mar 10, 2021

Copy link to clipboard

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

238

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

10 Replies 10
Adobe Community Professional ,
Mar 10, 2021 Mar 10, 2021

Copy link to clipboard

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

Copy link to clipboard

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
Adobe Community Professional ,
Mar 11, 2021 Mar 11, 2021

Copy link to clipboard

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

Copy link to clipboard

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
Adobe Community Professional ,
Mar 12, 2021 Mar 12, 2021

Copy link to clipboard

Copied

LATEST

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
Adobe Community Professional ,
Mar 10, 2021 Mar 10, 2021

Copy link to clipboard

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

Copy link to clipboard

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
Adobe Community Professional ,
Mar 11, 2021 Mar 11, 2021

Copy link to clipboard

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

Copy link to clipboard

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
Adobe Community Professional ,
Mar 11, 2021 Mar 11, 2021

Copy link to clipboard

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
Resources