• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

1.6K

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , 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;}}

 

 

Votes

Translate

Translate
Community Expert ,
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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
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 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
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;}}

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

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 = "";

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Hi Nesa,

I'm trying to do something similar to this what's being discussed in this thread, but it's not working for me.  I've tried adapting all of the code above but am having trouble.  Here's what I'm trying to do:

 

There are 10 rows which can be filled in but are not required.  I need to take the average of  the entries and ignore the blank fields.  Then I need the answer rounded to the nearest integer.

 

For example, (row1 + row2 + row6 + row 10)/4 and result rounded to a whole number.

 

I've tried several strands of code but I've never done this before and am completely new to Javascript so I'm a bit lost...  Can you help?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

Lets say your fields are named (row1,row2,row3-row10) as custom calculation script of field where you want to show result use something like this:

var total = 0;
var sum = 0;
for(var i=1; i<=10; i++){
if(this.getField("row"+i).valueAsString != "" && !isNaN(this.getField("row"+i).value)){
total++
sum += Number(this.getField("row"+i).value)}};
var result = Math.round(sum/total);
if(sum != 0)
event.value = result;
else event.value = "";

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Replace

event.value = 0;

with

event.value = "N/A";

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
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  

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

What script does you use now?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
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;

}

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Replace

event.value = 0;

with

event.value = "N/A";

Votes

Translate

Translate

Report

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

Copy link to clipboard

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;

 

}

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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