average ignoring empty fields & zero values
hi, in my form i want to calculate average in 40 text fields. fields names are AB-1, AB-2, AB-3, etc up to AB-40. i have found bellow code that really works but only issue is that code not ignoring zero values. so can u pls help me edit bellow code to ignore zero. thanks..
// Initialize variables
var num = 0;
var sum = 0;
// Loop through the input fields
for (var i = 1; i < 5; i++) {
var f = getField("AB-" + i);
if (f.valueAsString) {
// increment the non-blank field counter
num++;
// add the field value to the running total
sum += +f.value;
}
}
// Calculate the average
if (num) {
event.value = sum / num;
} else {
// All fields are empty, so set to blank
event.value = "";
}
