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?
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 = "";
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.
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 = "";
Copy link to clipboard
Copied
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;

