1 Reply Latest reply on Apr 14, 2014 1:06 PM by gkaiseril

    how to display text in addition to average group result


      Hi All,


      This is probably really simple but i need your help.


      What i have done:


      I have 4 questions which each one has 5 radio box options. For each group of questions, i have put a value of 1-5 for the radio boxes.


      What i am trying to do:


      What i want to do is calculate the total for the group results as an average and display some additional text. T


      The averaging for the group results is working, but i dont know how to add the additional text for the 1-5 values returned.


      For example, if the average for all 4 questions turns out to be 3, i want the text box to not only display 3 but state Moderate - 3; or something like 3 - Moderate.


      Any help would be greatly appreciated.


      Thank you,



        • 1. Re: how to display text in addition to average group result
          gkaiseril MVP & Adobe Community Professional

          Like in Excel you need to add some additional code but with JavaScript it is a lot more code. Unless you use a hidden field to hold the computed average, you need to provide all the code to perform the average, that is sum all the fields and divide by the number of fields. Once you have that value you can concatenate the text to the number. Or you could have one text field with the computed average and a second text field with the text. You will also have to deal with the average not always being a whole number. If you change the number of decimal places for your solution, you will see this.


          function GetField(cName) {
          // get object for field with cName;
          // return object or null if not found;
          var oField = this.getField(cName);
          if(oField == null) app.alert("Field named " +cName + " not found.", 0, 0);
          return oField;
          } // end GetField function;


          function Sum(aNames) {
          // get sum of the fields in aNamee array;
          var nSum = 0; // sum of fields;
          var nValue; // value or a single field;
          // process array of field names;
          for(i = 0; i < aNames.length; i++) {
          var nValue = GetField(aNames[i]).valueAsString;
          if(isNaN(nValue) == false) nSum += Number(nValue);
          } // get next field name;
          return nSum;
          } // end Sum function;


          function Average(aNames) {
          // compute average of the values of array of field names;
          var nAverage = "";
          var nSum = Sum(aNames); // get sum of field values;
          if(aNames.length > 0) nAverage = nSum / aNames.length;
          return nAverage;
          } // end Average function;


          // array of field names to average;

          var aFields = new Array("Value.0", "Value.1", "Value.2", "Value.3", "Value.4");
          // compute average using the function and place result in a variable;

          var nAverage = Average(aFields);

          // array of text for value of average;
          var aResult = new Array("Zero average", "Poor", "Better", "Mid point", "Better", "Best");
          // display computed average and text;

          event.value = nAverage + "  " + aResult[Math.floor(nAverage)];