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

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

New Here ,
Feb 21, 2018 Feb 21, 2018

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

TOPICS
Acrobat SDK and JavaScript , Windows

Views

606

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

Community Expert , Feb 21, 2018 Feb 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 ev

...

Votes

Translate

Translate
Community Expert ,
Feb 21, 2018 Feb 21, 2018

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

}

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
New Here ,
Feb 21, 2018 Feb 21, 2018

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?

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 Expert ,
Feb 21, 2018 Feb 21, 2018

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.

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
New Here ,
Feb 21, 2018 Feb 21, 2018

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?

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 Expert ,
Feb 21, 2018 Feb 21, 2018

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?

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
New Here ,
Feb 21, 2018 Feb 21, 2018

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.

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 Expert ,
Feb 21, 2018 Feb 21, 2018

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;

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
New Here ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

LATEST

That worked beautifully. Thank you

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
LEGEND ,
Feb 21, 2018 Feb 21, 2018

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?

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
New Here ,
Feb 21, 2018 Feb 21, 2018

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

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