
1. Re: using if statement to calculate field
maxwyss Jun 3, 2016 3:14 PM (in response to cherid433421)1 person found this helpfulThe 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*110000) * 0.05)) ;
and leave the fourth line away.
Would this lead to the correct numbers?

2. Re: using if statement to calculate field
cherid433421 Jun 3, 2016 3:34 PM (in response to maxwyss)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
cherid433421 Jun 3, 2016 3:38 PM (in response to maxwyss)1 person found this helpfulThere was one parenthesis missing in your original formula, the correct one would be:
event.value = Math.max(400, (400 + ((a.value*110000) * 0.05)) );

4. Re: using if statement to calculate field
maxwyss Jun 3, 2016 5:28 PM (in response to cherid433421)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
maxwyss Jun 3, 2016 5:37 PM (in response to cherid433421)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
cherid433421 Jun 6, 2016 12:06 PM (in response to maxwyss)Thank you very much for all of your help! This is extremely useful.