Copy link to clipboard
Copied
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);;
}
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 ev
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
It does calculate. It assigns an empty value in that case, since division by zero is not an allowed operation.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
The code I provided will prevent it from happening. What should be the result if p2 or p3 is zero, in your opinion?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
That worked beautifully. Thank you
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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);
}