3 Replies Latest reply on Feb 20, 2012 1:21 AM by try67

    Javascript 'if' calculations




      I need some help with a javascript calculation please. I am working on using a weight to calculate the field 'Ibu', but would like it to max out at a certain value. Eg up to the weight of 53.3kg, the value returned would be calculated according to the weight multiplied by 0.375. Beyond that, the value will always be 20. How can I do this? The field reference is called 'Weight' and the calculation is Weight*0.375. I assume I would use an 'If, else' statement, but beyond that I am totally clueless - have tried googling but I'm kinda hopeless with java (I could do basic way back when lol).


      Many thanks in advance,


        • 1. Re: Javascript 'if' calculations
          George_Johnson MVP & Adobe Community Professional

          The custom calculation script for the field would be something like the following:


          (function () {
              // Get the value of the Weight field
              var v1 = getField("Weight").value;
              // Perform the calculation
              var  v2 = 0.375 * v1;
              // Set this field value
              event.value = v2 > 20 ? 20 : v2;


          That last line is equivalent to:


          if (v2 > 20) {
              event.value = 20;
          } else {
              event.value = v2;
          • 2. Re: Javascript 'if' calculations
            SaqibH Level 1

            you, sir, are a legend! I have been trying for ages to work it out using the if else system and just couldn't do it, didn't think about using this way instead (well I did, but thought i would need a totally new argument!). Man basic was so much easier lol


            Many thanks again.

            • 3. Re: Javascript 'if' calculations
              try67 MVP & Adobe Community Professional

              First of all, it's JavaScript, not just Java. The two are different...

              And yes, you need a simple if-else statement, like this:


              var weight = +this.getField("Weight").value;

              var lby = 0;

              if (weight>53.53)

                   lbu = 20;

              else lbu = weight * 0.375;


              event.value = lbu;

              1 person found this helpful