4 Replies Latest reply on Aug 2, 2014 1:40 PM by Asatani

    Addition script giving wrong numbers

    Asatani

      I have a script that hide a value if the value of another field is blank. I have two other fields performing the same function (with different respective fields) and the only difference between them and this one is that this one adds one more field. I will list a couple different outputs to show its behavior and a link to the script I used, though I see nothing wrong with the script itself. Also, the field order is where it should be. It does some really funky things with my totals that the other scripts don't experience. Pastebin

       

      Base: 5
      Agility: -30
      Special: (blank)

      Class: 5

      Final: -255

       

      Base: 5
      Agility: -30
      Special: 5

      Class: 5

      Final: -15

       

      Base: 5
      Agility: -30
      Special: (blank)

      Class: (blank)

      Final: -25

       

      Base: 5
      Agility: 0
      Special: (blank)

      Class: 5

      Final: 55

       

      Base: 156
      Agility: 0
      Special: (blank)

      Class: 5

      Final: 1,565

        • 1. Re: Addition script giving wrong numbers
          try67 MVP & Adobe Community Professional

          You need to make sure the values you're adding are numbers. Use this code:

           

          event.value =

              Number(this.getField("Page1_Dodge_Base").value) +

              Number(this.getField("Page1_Dodge_Agility_Bonus").value) +

              Number(this.getField("Page1_Dodge_Special").value) +

              Number(this.getField("Page1_Dodge_Class").value);

          • 2. Re: Addition script giving wrong numbers
            Asatani Level 1

            Thank you, that did the trick! However, I must ask... Why is that the very same script works fine when it is adding only three values, but fails to work for this instance. For clarification, the other fields have positives, negatives and blanks, as well. They work just fine.

            • 3. Re: Addition script giving wrong numbers
              try67 MVP & Adobe Community Professional

              If any of the values that are being added is a string (for example, if it's blank), then the "+" operator treats the entire thing as a string and concatenates the values, instead of adding them up.

               

              So when you're adding these values: 156+0+""+5

              The result is "1565" because it's adding it up like this :

              156+0 = 156

              156 + "" = "156"

              "156" + 5  = "1565"

              • 4. Re: Addition script giving wrong numbers
                Asatani Level 1

                Ah! I think I understand. In the scripts that appeared to work, the blanks were the last values being added. Theoretically, if I throw a blank in the middle, they should react the same way this one was.