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

Calculated values not calculating correctly all every time

New Here ,
Jan 18, 2024 Jan 18, 2024

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

For comparisons use ==, not =.

For example:

if (size=="1/6")

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

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.

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

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

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

...

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

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="";
//

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

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.

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

Translate
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