Dividing by a field in Adobe Form Syntax Error

New Here ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

Total JS newb here. Sol, I apologize in advance.  I keep getting a syntax error 4 at line 5 on this code.  Basically, I want to multiply a field by a constant and divide by another field.  If the divisor field is zero or blank, I want the output to be "N/A".  What have I screwed up?  TIA.

 

var nA = Number(this.getField("LogTot1").valueAsString);
var nB = Number(this.getField("Hours1").valueAsString);
event.value = "NA";
if(var nB=="" || var nB=="0"){
} else {
event.value = (nA*200000)/nB;}

 

TOPICS
JavaScript , PDF forms

Views

45

Likes

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 , Sep 01, 2022 Sep 01, 2022

The variables need to be declared only once.

var nA = Number(this.getField("LogTot1").valueAsString);
var nB = Number(this.getField("Hours1").valueAsString);

if(nB=="" || nB=="0"){
    //do something
    event.value = "NA";
} else {
    // do something else
    event.value = (nA*200000)/nB;
}

 

Likes

Translate

Translate
Community Expert ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

The variables need to be declared only once.

var nA = Number(this.getField("LogTot1").valueAsString);
var nB = Number(this.getField("Hours1").valueAsString);

if(nB=="" || nB=="0"){
    //do something
    event.value = "NA";
} else {
    // do something else
    event.value = (nA*200000)/nB;
}

 

Likes

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 ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

Thanks for the quick response!  I'm sorry but I'm still a rookie at this stuff.  Not sure what you mean.  I thought I had only defined the variables once with the first two lines?

Likes

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 ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

Oh shoot.  Nevermind.  I see what you did there.  Thank you so much!! 

Likes

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 ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

This line is problematic, since you've declared this variable as a Number:

if (nB=="" || nB=="0"){

It will work, because JS does the class casting for you from string to number, but it's not a good way of doing it.

Instead just compare it to zero, like this:

if (nB==0) {

Likes

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 ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

LATEST

Great thank you as well!

Likes

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