Copy link to clipboard
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="";//
Copy link to clipboard
Copied
For comparisons use ==, not =.
For example:
if (size=="1/6")
Copy link to clipboard
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.
Copy link to clipboard
Copied
Check the fields calculation order. If it still doesn't work, please share the actual file with us.
Copy link to clipboard
Copied
...
Copy link to clipboard
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="";
//
Copy link to clipboard
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.
Copy link to clipboard
Copied
Here's an article on how to write an "if" statement.
https://www.pdfscripting.com/public/How-to-write-an-If-statement.cfm
Find more inspiration, events, and resources on the new Adobe Community
Explore Now