21 Replies Latest reply on Oct 21, 2009 12:33 PM by How do it know

    Custom Script Calculation: Average

    How do it know

      How do I find an average for multiple text fields that can contain "na" in some fields and a value range of 1 thru 5?  Please help

       

      The following is not working:

       

      var aNumFields = new Array(Text11CS1, Text12CS2, Text13CS3, Text14CS4);

                  myAverageFunction(aNumFields);

                  function myAverageFunction(aNumFields)

                  {

                              // n = number of fields that have a numerical value

                              var n=0, sum = 0;

                              for ( var i=0; i<aNumFields.length; i++) {

                                          var v = this.getField(aNumFields[i]).value;

                                                      if ( v != "" ) {

                                                                              n++;

                                                                              sum += v;

                                                      }

                              }

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

                              else event.value = sum/n;   

                  }