• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

using if statement to calculate field

Community Beginner ,
Jun 03, 2016 Jun 03, 2016

Copy link to clipboard

Copied

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;};

TOPICS
Acrobat SDK and JavaScript

Views

809

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Jun 03, 2016 Jun 03, 2016

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?

Votes

Translate

Translate
Advocate ,
Jun 03, 2016 Jun 03, 2016

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 03, 2016 Jun 03, 2016

Copy link to clipboard

Copied

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;};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jun 03, 2016 Jun 03, 2016

Copy link to clipboard

Copied

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…

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 06, 2016 Jun 06, 2016

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 03, 2016 Jun 03, 2016

Copy link to clipboard

Copied

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)) );

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jun 03, 2016 Jun 03, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines