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

Calculate Average and ignore zero/blank.

Community Beginner ,
May 19, 2023 May 19, 2023

Copy link to clipboard

Copied

So I should start by saying that I've tried to find and/or adapt code within the discussion boards for the past couple of days. However, I keep getting syntax errors, or the code doesn't work. 

Anyways, I have a form with 6 boxes for scores. How can I code the 7th box to be the average of the 6 boxes and ignore blanks or zeros? 

 

TOPICS
How to , PDF , PDF forms

Views

777
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
1 ACCEPTED SOLUTION
Community Expert ,
May 19, 2023 May 19, 2023

Copy link to clipboard

Copied

EDITED:

As 'Custom calculation script' of '2g' field, use this:

 

var total = 0;
var avg = 0;
for(var i=1; i<=6; i++){
if(this.getField("2e"+i).valueAsString != "" && Number(this.getField("2e"+i).valueAsString) != 0){
total += Number(this.getField("2e"+i).valueAsString);
avg++;}}

if(avg != 0)
event.value = total/avg;
else
event.value = "";

 

 

View solution in original post

Votes

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 Beginner ,
May 19, 2023 May 19, 2023

Copy link to clipboard

Copied

Here is what the form looks like. 2.a.6. is not always required. The average for 2.a.1-2.a5. is easy. However, I want 2.g to consider the score for 2.a.6. when it is used. 

 

Capture.JPGexpand image

Votes

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 ,
May 19, 2023 May 19, 2023

Copy link to clipboard

Copied

EDITED:

As 'Custom calculation script' of '2g' field, use this:

 

var total = 0;
var avg = 0;
for(var i=1; i<=6; i++){
if(this.getField("2e"+i).valueAsString != "" && Number(this.getField("2e"+i).valueAsString) != 0){
total += Number(this.getField("2e"+i).valueAsString);
avg++;}}

if(avg != 0)
event.value = total/avg;
else
event.value = "";

 

 

Votes

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 Beginner ,
May 19, 2023 May 19, 2023

Copy link to clipboard

Copied

LATEST

Nesa, 

I appreciate the input. I actually decided to give ChatGPT a try. It gave me a code that worked like a charm! 

 

// Custom calculation script for Adobe Acrobat
// Calculate average of six numerical values, ignoring blanks and zeros

var values = new Array(
getField("2e1").value,
getField("2e2").value,
getField("2e3").value,
getField("2e4").value,
getField("2e5").value,
getField("2e6").value
);

var sum = 0;
var count = 0;

for (var i = 0; i < values.length; i++) {
var value = parseFloat(values[i]);

if (!isNaN(value) && value !== 0) {
sum += value;
count++;
}
}

var average = count === 0 ? 0 : sum / count;

event.value = average;

Votes

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