10 Replies Latest reply on Feb 21, 2018 2:17 PM by keviny45

Trying to write a Javascript to compare the lesser amount of two fields (p2 & p3) to perfom a calculation that divides the lesser of the two fields by a 3rd field (p1) that will display the results in the field I'm writing the script. Below doesnt work

var x = this.getField("p2").value;
var y = this.getField("p3").value;

if (p2 < p3) {
event.value = "p1 / p2";
}
else {
event.value = (p1 / p3).toFixed(2);;
}

• 1. Re: Trying to write a Javascript to compare the lesser amount of two fields (p2 & p3) to perfom a calculation that divides the lesser of the two fields by a 3rd field (p1) that will display the results in the field I'm writing the script. Below doesnt wor

Couple of issues there:

1. Drop the quotes around "p1 / p2".

2. You should explicitly convert the values to numbers.

3. If the denominator is 0 you can't divide by it.

4. You're not using the variables you declared in the first two lines.

5. You're missing the definition of p1.

So use this code:

```var p1 = Number(this.getField("p1").valueAsString);
var p2 = Number(this.getField("p2").valueAsString);
var p3 = Number(this.getField("p3").valueAsString);

if (p2 < p3) {
if (p2==0) event.value = "";
else event.value = p1 / p2;
} else {
if (p3==0) event.value = "";
else event.value = (p1 / p3).toFixed(2);
}
```
• 2. Re: Trying to write a Javascript to compare the lesser amount of two fields (p2 & p3) to perfom a calculation that divides the lesser of the two fields by a 3rd field (p1) that will display the results in the field I'm writing the script. Below doesnt wor

There are a number of problems with the code, but before we get to that you need to clarify the calculation you want to perform. You say "...a calculation that divides the lesser of the two fields (p2 & p3) by a 3rd field (p1)..."

To me, this means either:

p2 / p1

or:

p3 / p1

Yet the code seems to indicate either:

p1 / p2

or:

p1 / p3

Can you clarify?

• 3. Re: Trying to write a Javascript to compare the lesser amount of two fields (p2 & p3) to perfom a calculation that divides the lesser of the two fields by a 3rd field (p1) that will display the results in the field I'm writing the script. Below doesnt wor

Thank you.... That works perfect when there are values in both P2 & P3. However, there will be occasions when either p2 or p3 will not have an input amount. In this case, it does not calculate. Is there a fix for that?

• 4. Re: Trying to write a Javascript to compare the lesser amount of two fields (p2 & p3) to perfom a calculation that divides the lesser of the two fields by a 3rd field (p1) that will display the results in the field I'm writing the script. Below doesnt wor

Some occasions, user will not enter a number in either P2 or P3, in this case the script below does not calculate. Is there something that could be added to the script for that?

var p1 = Number(this.getField("p1").valueAsString);
var p2 = Number(this.getField("p2").valueAsString);
var p3 = Number(this.getField("p3").valueAsString);

if (p2 < p3) {
if (p2==0) event.value = "";
else event.value = p1 / p2;
} else {
if (p3==0) event.value = "";
else event.value = (p1 / p3).toFixed(2);
}

• 5. Re: Trying to write a Javascript to compare the lesser amount of two fields (p2 & p3) to perfom a calculation that divides the lesser of the two fields by a 3rd field (p1) that will display the results in the field I'm writing the script. Below doesnt wor

It does calculate. It assigns an empty value in that case, since division by zero is not an allowed operation.

• 6. Re: Trying to write a Javascript to compare the lesser amount of two fields (p2 & p3) to perfom a calculation that divides the lesser of the two fields by a 3rd field (p1) that will display the results in the field I'm writing the script. Below doesnt wor

It seems I read somewhere that there was a get around that you could add to your code to get around the "division by zero not allowed". Is there such a thing?

• 7. Re: Trying to write a Javascript to compare the lesser amount of two fields (p2 & p3) to perfom a calculation that divides the lesser of the two fields by a 3rd field (p1) that will display the results in the field I'm writing the script. Below doesnt wor

The code I provided will prevent it from happening. What should be the result if p2 or p3 is zero, in your opinion?

• 8. Re: Trying to write a Javascript to compare the lesser amount of two fields (p2 & p3) to perfom a calculation that divides the lesser of the two fields by a 3rd field (p1) that will display the results in the field I'm writing the script. Below doesnt wor

I see what you mean. I guess I was hoping if there was not a input at all, field left blank in p3 that the division calculation would default to dividing p1 / p2 because p2 will always have a input. p3 will not. I don't know if you can write a code for that or not. is there? I'm just getting started with JavaScript.

• 9. Re: Trying to write a Javascript to compare the lesser amount of two fields (p2 & p3) to perfom a calculation that divides the lesser of the two fields by a 3rd field (p1) that will display the results in the field I'm writing the script. Below doesnt wor

Do you mean in the second condition, if p3 is empty (or zero) it should divide by p2, instead? That's possible, yes.

Replace line #9:

if (p3==0) event.value = "";

With this:

if (p3==0) event.value = p1/p2;

• 10. Re: Trying to write a Javascript to compare the lesser amount of two fields (p2 & p3) to perfom a calculation that divides the lesser of the two fields by a 3rd field (p1) that will display the results in the field I'm writing the script. Below doesnt wor

That worked beautifully. Thank you