Skip to main content
Participating Frequently
September 25, 2017
Answered

Formula to calculate the average of a number of fields

  • September 25, 2017
  • 2 replies
  • 7732 views

I am trying to write a formula in java script to calculate the average of 42 numbers, where some of the fields can be empty.

This topic has been closed for replies.
Correct answer try67

I've developed a function that allows you to do it easily. The basic code is:

 

/*

    @17537118

        aFields : an array of the field names to use for the calcluation

        bIgnoreBlanks : a boolean that tells the function whether or not to ignore blank fields

        bIgnoreZeros : a boolean that tells the function whether or not to ignore zero values

*/

function calcAverage(aFields, bIgnoreBlanks, bIgnoreZeros) {

    var total = 0;

    var n = 0;

    for (var i in aFields) {

        var f = this.getField(aFields[i]);

        if (f==null) {

            console.println("Error! Can't locate a field called: " + aFields[i]);

            continue;

        }

        if (f.valueAsString=="" && bIgnoreBlanks) continue;

        var v = Number(f.valueAsString);

        if (isNaN(v)) continue;

        if (v==0 && bIgnoreZeros) continue;

        total+=v;

        n++;

    }

    if (n==0) event.value = "";

    else event.value = total/n;

}

 

You can then call it from the field's calculation script like this:

calcAverage(["Field1", "Field2", "Field3"], true, false);

 

Edit: Fixed some small spelling mistakes in the code

2 replies

Participating Frequently
December 17, 2018

Could someone explain much more simple for beginners

I want to make cusomer saisfaction form which has 8 criteria and rate from 1 to 6 (built by Form) that means the value receives from cusomer and at the end will calculate the avarage of given points. Normally the default avarage program works well but I have a "not to be rated" field that mean the criteria should be ignored in the calculation!

I got the code and modified it but still doesn't work . (I made field and in field properties activate calculation and choosed the last option customize calculation and added the code , but doesnt work at all.

could someone help me?

BR

try67
Community Expert
Community Expert
December 17, 2018

Did you use the code I shared above? If so, how did you call it, and what were the exact results? Saying "it didn't work" is not very helpful to us. You need to provide more details.

Participating Frequently
December 17, 2018

I tried 2 ways :

1- I made a field and in calculation I choosed the 3rd option "custom calculation script"

2-Second way : I added the calculation function in Javascript tool

and call the function on the custom calculation i the field.

in both ways nothing showed up!

try67
Community Expert
Community Expert
September 25, 2017

If a field is empty, do you want to treat it as zero and include it in the calculation, or ignore it?

mlbooth78Author
Participating Frequently
September 25, 2017

If the field is empty I would like for it to be ignored.