
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
try67 Feb 21, 2018 9:02 AM (in response to keviny45)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
George_Johnson Feb 21, 2018 9:05 AM (in response to keviny45)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
keviny45 Feb 21, 2018 10:11 AM (in response to try67)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
keviny45 Feb 21, 2018 11:18 AM (in response to keviny45)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
try67 Feb 21, 2018 12:15 PM (in response to keviny45)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
keviny45 Feb 21, 2018 1:18 PM (in response to try67)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
try67 Feb 21, 2018 1:26 PM (in response to keviny45)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
keviny45 Feb 21, 2018 1:50 PM (in response to try67)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
try67 Feb 21, 2018 1:55 PM (in response to keviny45)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
keviny45 Feb 21, 2018 2:17 PM (in response to try67)That worked beautifully. Thank you