6 Replies Latest reply on Jun 6, 2016 12:06 PM by cherid433421

# using if statement to calculate field

I have a somewhat complex formula to calculate on a form. In English, the formula is as follows:

(Population Served - 10,000) * .05 + \$400

With \$400 being the base amount so anything equal to or less than 10,000 should just be \$400

Below is the javascript I have on my field. It works great when I enter, "1," "9000," "450," but if I enter "5000" it returns a value of \$150. I'm really confused by what is happening so I must be missing something in the code:

var a = this.getField("Population");

var b = this.getField("PopulationTotal");

b.value = (a.value - 10000) *.05 + 400;

if (a.value < 10000) {event.value = 400;};

• ###### 1. Re: using if statement to calculate field

The fundamental error in the coding is that the result should be compared with 400\$, and not the population number value.

Try this line:

event.value = Math.max(400, (400 + ((a.value*1-10000) * 0.05)) ;

and leave the fourth line away.

Would this lead to the correct numbers?

• ###### 2. Re: using if statement to calculate field

I had just found a solution that worked (using "b.value" instead of "event.value" in the if statement), but your solution worked as well with less code. Thank you!

An additional question for you, if Math.max(400) means that \$400 is the minimum value for the field, would Math.min return anything \$400 and under?

I ask because I have an additional field that has both a minimum and a maximum value. I found a solution with the below javascript, but your earlier solution was so much cleaner and simpler, I'm just wondering if it would work for this one:

var a = this.getField("TotalWaterProducedGallons");

var b = this.getField("TotalWaterProducedAcreFeet");

var c = this.getField("TotalWaterProducedLiters");

var d = this.getField("PopulationTotal");

var e = this.getField("TOTALDrinkingWaterCommitment");

e.value = a.value + b.value + c.value + d.value;

if (e.value < 1090) {e.value = 1090;};

if (e.value > 561073) {e.value = 561073;};

• ###### 3. Re: using if statement to calculate field

There was one parenthesis missing in your original formula, the correct one would be:

event.value = Math.max(400, (400 + ((a.value*1-10000) * 0.05)) );

• ###### 4. Re: using if statement to calculate field

Thanks for the mention… Unfortunately, I can no longer edit the message; otherwise, I would have updated it.

• ###### 5. Re: using if statement to calculate field

Math.max(a, b) returns the greater value of the two.

Math.min(a, b) returns the smaller value of the two.

(actually, you can pass more than two arguments, and it will return the greatest or smallest of them all).

For your example, may I assume that the searched result is filled into field "TOTALDrinkingWaterCommitment".

In this case, I would do the following

var daSum = a.value*1 + b.value*1 + c.value*1 + d.value*1 ;

event.value = Math.min(561073, Math.max(1090, daSum)) ;

And that should do it (and I did count the parentheses…).

Note the multiplication by 1 for the four summands. This forces the values to be numbers, so that they are actually added. Otherwise, as soon as one field is empty, the value would be the empty string, and instead of adding up, the numbers would be concatenated. …welcome to the great world of JavaScript…

• ###### 6. Re: using if statement to calculate field

Thank you very much for all of your help! This is extremely useful.