Get the Average of the rows with value. Ignore the 0 and empty field
- June 23, 2023
- 4 replies
- 2689 views
I use this code I saw in the discussions but it didn't work.
I need to get the average of each field with data and ignore the empty or 0 fields.
myAverageFunction(aNames)
{
// Initialize variables
var i, v, num = 0, sum = 0;
// Loop through the input fields
for (i = 1; i <= 21; i++)
{
v = +getField("AvgCohRow1_."+ i).value;
if (v!== 0) {
// increment the non-blank/zero field counter
num++;
// add the field value to the running total
sum += v;
}
}
// Calculate the average
if (num) {
event.value = sum / num;
} else {
// All fields are empty, so set to blank
event.value = "";
}
}
event.value = myAverageFunction(["AvgCohRow1_1", "AvgCohRow1_2", "AvgCohRow1_3", "AvgCohRow1_4", "AvgCohRow1_5", "AvgCohRow1_6", "AvgCohRow1_7", "AvgCohRow1_8", "AvgCohRow1_9", "AvgCohRow1_10", "AvgCohRow1_11", "AvgCohRow1_12", "AvgCohRow1_13", "AvgCohRow1_14", "AvgCohRow1_15", "AvgCohRow1_16", "AvgCohRow1_17", "AvgCohRow1_18", "AvgCohRow1_19", "AvgCohRow1_20", "AvgCohRow1_21"]);
