Skip to main content
keviny45
Participating Frequently
February 21, 2018
Answered

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 wo

  • February 21, 2018
  • 3 replies
  • 1202 views

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

This topic has been closed for replies.
Correct answer try67

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

}

3 replies

keviny45
keviny45Author
Participating Frequently
February 21, 2018

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

Inspiring
February 21, 2018

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?

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
February 21, 2018

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

}

keviny45
keviny45Author
Participating Frequently
February 21, 2018

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?

try67
Community Expert
Community Expert
February 21, 2018

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