• Global community
• Language:
• Deutsch
• English
• EspaÃ±ol
• FranÃ§ais
• PortuguÃªs
• æ—¥æœ¬èªžã‚³ãƒŸãƒ¥ãƒ‹ãƒ†ã‚£
Dedicated community for Japanese speakers
• í•œêµ­ ì»¤ë®¤ë‹ˆí‹°
Dedicated community for Korean speakers
Exit
• Calculated values not calculating correctly all ev...

# Calculated values not calculating correctly all every time

New Here ,
Jan 18, 2024 Jan 18, 2024

Copied

If written the following script and I've found that it's working some of the time but not all of the time. For example when I have the size as 1/6 and the cost anywhere from 275-396 the value should be 7.00 but it puts it as 5.00. When I type 397 it changes to 7.00. I can't determine why it's not calculating correctly every time.

var size=(this.getField("KegSize").value);
var cost=Number(this.getField("KegCost").value);
var ABV=Number(this.getField("ABV").value);
if (size="1/6")var product1=Number(cost/640);
if (size="1/4")var product2=Number(cost/992);
if (size="1/2")var product3=Number(cost/1984);
if ((ABV>1)&&(product1>.2))event.value="7.00"; else if ((ABV>1)&&(product1<.2))event.value="5.00"; else if (ABV<1) event.value="";
if ((ABV>1)&&(product2>.2))event.value="7.00"; else if ((ABV>1)&&(product2<.2))event.value="5.00"; else if (ABV<1) event.value="";
if ((ABV>1)&&(product3>.2))event.value="7.00"; else if ((ABV>1)&&(product3<.2))event.value="5.00"; else if (ABV<1) event.value="";//

TOPICS
Acrobat SDK and JavaScript

Views

262

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
7 Replies 7
Community Expert ,
Jan 18, 2024 Jan 18, 2024

Copied

For comparisons use ==, not =.

For example:

if (size=="1/6")

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jan 18, 2024 Jan 18, 2024

Copied

Thank you. I changed that and it still isn't calculating correctly. It's so random how some numbers give the correct values and others don't. If it was totally broken that would be one thing but it switches back and forth between 5 and 7 - just not always correctly.

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 Expert ,
Jan 18, 2024 Jan 18, 2024

Copied

Check the fields calculation order. If it still doesn't work, please share the actual file with us.

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 Expert ,
Jan 18, 2024 Jan 18, 2024

Copied

...

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jan 18, 2024 Jan 18, 2024

Copied

So I realized that it was calculating both the 1/6 and the 1/2 any time the size was 1/6. Not sure what that was happening, but I've rewritten the code as follows and it seems to be working. I couldn't seem to find a way to get the first set of code to not calculate multiple lines.

var size=(this.getField("KegSize").value);
var cost=Number(this.getField("KegCost").value);
var ABV=Number(this.getField("ABV").value);
if (size=="1/6")
if ((ABV>1)&&(Number(cost/640)>.2))event.value=7; else if ((ABV>1)&&(Number(cost/640)<.2))event.value=5; else event.value="";
;
if (size=="1/4")
if ((ABV>1)&&(Number(cost/992)>.2))event.value=7; else if ((ABV>1)&&(Number(cost/992)<.2))event.value=5; else event.value="";
;
if (size=="1/2")
if ((ABV>1)&&(Number(cost/1984)>.2))event.value=7; else if ((ABV>1)&&(Number(cost/1984)<.2))event.value=5; else event.value="";
;
if (ABV<1) event.value="";
if ((size=="")||(size!=="1/6")||(size!=="1/4")||(size!=="1/2")) event.value="";
//

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 Expert ,
Jan 18, 2024 Jan 18, 2024

Copied

Your conditions are overwriting one another. Either combine them all to a single if-else if-else statement, or remove the final else clause in each one.

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 Expert ,
Jan 18, 2024 Jan 18, 2024

Copied

LATEST

Here's an article on how to write an "if" statement.

https://www.pdfscripting.com/public/How-to-write-an-If-statement.cfm

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often