4 Replies Latest reply on Jun 24, 2014 12:25 PM by Rhino111

    Custom JavaScript to omit NA selection.

    Rhino111

      I have a form that uses 5 sections of combo boxes. In section has 4 sets of combo boxes and you have the option to choose 0,1,2,3, and NA. Beside each of those combo boxes is a score which reflects what you have chosen in each combo box. At the end of each section is a score which gives you an average of the above 4 choices.

       

      I need to write in custom JavaScript that lets you get the average if the user selects NA. As NA does not count in the score. If the user selects NA the score would now be out of 3 not 4 and so on depending on hos many times the user selects NA.

       

      I will also need custom JavaScript in the final section that gives me the average of all final scores and omits the NA as well. Could someone please help me with this custom JavaScript as I have no knowledge of JavaScript only that I need it.

        • 1. Re: Custom JavaScript to omit NA selection.
          MatLac Level 1

          in the calculate event of "total":

           

          var aField = new Array("field1", "field2", etc)  //create an array of the fields you need to check the value of

          var aScore = new Array();  //creating an empty array for further use

          var nScore = 0;  //creating a variable holding the sum of the scores

          for (var i = 0; i < aField.length; i++){ //looping through every item of array aField

               if (this.getField(aField[i]).value > 0){  //if our value is a number eliminating 0, nothing and NA

                    aScore.push(aField[i]);  //we will count the number of correct values with this array;

                    nScore += Number(this.getField(aField[i]).value);  //  we add the value to our total

               }

          }

          if (aScore.length >0){  //We don't want to divide our score by 0 because it's impossible

               event.value = nScore/aScore.length;  //we devide the result by the number of correct values in the array.

          }

           

          you can adapt that script to use in every "total" fields including the GRAND TOTAL one

          • 2. Re: Custom JavaScript to omit NA selection.
            Rhino111 Level 1

            This is great, however it's not calculating the score of "0". Just like NA. It removes "0" from the equation. For example if I get scores of 3, 3, 3, 0 it equals 3. I need it to equal 2.25.

            • 3. Re: Custom JavaScript to omit NA selection.
              MatLac Level 1

              Of course, a little error on my part.  Then change this line:

               

              if (this.getField(aField[i]).value > 0){


              to this line:


              if (this.getField(aField[i]).value >= 0){


              So anything larger OR EQUAL to 0 will be considered.

              • 4. Re: Custom JavaScript to omit NA selection.
                Rhino111 Level 1

                Perfect, really appreciate it.