2 Replies Latest reply on May 3, 2011 8:07 AM by csb102155

    Simple Javascript calculation

    csb102155 Level 1

      Hi - I need help with a simple javascript calculation. I wrote this javascript:

      var cons = this.getField("Text6.0")
      var cons = this.getField("Text5.0")
      if (plan.value != 0)
      event.value = cons.value - plan.value / plan.value
      event.value = ""


      However, the calculation doesn't work right.  For Example  105 - 100 / 100 should equal to .05%, however, the script comes out to 0.00%.  Is there a better way to write a script so that the answer comes to .05%?


        • 1. Re: Simple Javascript calculation
          George_Johnson MVP & Adobe Community Professional

          You need to clarify a few things. Your first two lines are setting the value of the same variable to the same thing. I'm assuming one of them should be the "plan" variable, but which one?


          Second, you need to clarify what the calculation sholw be. Should it be:


          (105 - 100) / 100 = 5 / 100 = 0.05


          If so, the code would be:


          event.value = (cons.value - plan.value) / plan.value;


          But you have to first check to see that plans.value does not evaluate to zero before you execute the line of code. This will happen when the plan field is blank, for example. What should the value of the field be when plan is blank or zero?

          • 2. Re: Simple Javascript calculation
            csb102155 Level 1

            Thanks George, the script you provided worked.