20 Replies Latest reply on Oct 16, 2016 3:05 PM by Karl Heinz Kremer

# Help! help! How come Math pow( ) cannot work on this????

Hi, I have no Java Script experience, wanted to create more complex calculation on DC Acrobat Pro, but only know how to perform basic calculation by using simplified script. Please see attached.

assuming I created field for No. of years, Monthly Amount, Total amount (for row of the table)

Here are my script

12*Monthly Amount*Math.pow(1-1.02,No. of years)/1-1.02

12=one year

1.02=interest p.a

But it does not work. Does Math.pow work on acrobat or can I have any simply way to work on this??

• ###### 1. Re: Help! help! How come Math pow( ) cannot work on this????

Math.pow cannot be used in the simplified field notation. Yo need to use the custom JavaScript calculation option.

You cannot have blanks, spaces, or special characters in field names using simplified field notatio unless you use the JavaScript Escape character.

Are you getting any error messages in the JavaScript console?

• ###### 2. Re: Help! help! How come Math pow( ) cannot work on this????

Retrying on the custom JavaScipt calculation option.

12*m*(Math.pow(1-1.02,S))/1-1.02

m=monthly amount

S=no. of year

still not working for both option.

No error messages in the JavaScript console for both simplified and custom option.

• ###### 3. Re: Help! help! How come Math pow( ) cannot work on this????

Use Ctrl +J keys to open the JavaScript console. Bothscripts are generating errors.

In JavaScript you need to access the field object using "getField" and then the value property of the field object.

• ###### 4. Re: Help! help! How come Math pow( ) cannot work on this????

Thanks!

This is what i have from the JavaScript Debugger:

ReferenceError: m is not defined

1:Field:Calculate

ReferenceError: m is not defined

1:Field:Calculate

for m is not defined? what does that mean already created the field??

• ###### 5. Re: Help! help! How come Math pow( ) cannot work on this????

You did not access the field properly.

• ###### 6. Re: Help! help! How come Math pow( ) cannot work on this????

Hi, Sorry, do you have any other way to solve this

maybe my script is not the correct way to work.

because if I use S*m it will turn out correctly (S=No of year) x(M=Monthly amount)

but adding the Math.pow it will...encounter problem.

Basically, it is supose to work on the row 1 (see attached hightlighted)

Each year (12 months) x No of years (eg 2 years) x Monthly amount (1,000) x (1- the interest rate (1.02)Math pow of (no of year(2 year) divided by 1-1-0.02) total amount of the first row will be S\$24240.

Regards

Thanks

• ###### 7. Re: Help! help! How come Math pow( ) cannot work on this????

Sorry, should be

Each year (12 months) x No of years (eg 2 years) x Monthly amount (1,000) x (1- the interest rate (1.02)Math pow of (no of year(2 year) divided by 1-1.02) total amount of the first row will be S\$24240.

• ###### 8. Re: Help! help! How come Math pow( ) cannot work on this????

1. Converting from simplified field notation to full JavaScript is more than just choosing the option. Your JavaScript must do some of the things that were automatic in the simplified form. In particular, you must CONNECT every field you work with to a variable (except in some kinds of event, where you must NOT do this and have a special variable instead). Commonly, one uses getField to connect to variables. You must use "m" expecting it to contain the value of the field "m" - no, you can't, and that's why it is undefined.

2. Obviously something is very wrong with the formula as you've expressed it.  1-1.02 is always -0.02.

So for one year the result is pow(-0.02,1) = -0.02. For two years, pow(-0.02,2) = -0.02 * -0.02 = +0.004. For three years, pow(-0.02,3) = -0.02 * -0.02 * -0.02 = -0.0008. So your results are alternatively negative and positive, and rapidly get very small indeed. This is not a JavaScript issue, but a mathematical misunderstanding of something, to solve before coding.

• ###### 9. Re: Help! help! How come Math pow( ) cannot work on this????

I think you misunderstand the math pow part.

It should be 1.02 pow of (2 years,3 years ...etc)(is just the 1.02 pow of...)

result for one year 1-1.02=-0.02, 2 years=1- (1.02 * 1.02)= -0.0404, 3 years=1-(1.02 * 1.02 * 1.02)=0.06121

Or try this ways

Monthly amount (1,000) x ( the interest rate (1.02)Math pow of (no of year(2 year)-1, divided by 0.02) x Each year (12 months) x No of years (eg 2 years) = total amount of the first row will be S\$24240.

thanks

• ###### 10. Re: Help! help! How come Math pow( ) cannot work on this????

I think I understood what the Math.pow part actually DID. I didn't understand what you wanted to do. You wrote

Math.pow(1-1.02,No. of years)

which gives the result I said. Because the first thing that must happen is 1-1.02 so what you write is EXACTLY the same as

Math.pow(-0.02,No. of years)

You probably want something more like

1-Math.pow(1.02,No. of years)

• ###### 11. Re: Help! help! How come Math pow( ) cannot work on this????

Hi, Sorry for not interpret properly.

Yap, this is what i mean

1-Math.pow(1.02,No. of years)

So one year 1-1.02=-0.02, 2 years=1- (1.02 * 1.02)= -0.0404, 3 years=1-(1.02 * 1.02 * 1.02)=0.06121

Will it it work for coding? Really appreciate for your patients.

Thank you.

• ###### 12. Re: Help! help! How come Math pow( ) cannot work on this????

Try it, check the result, if it doesn't work please copy and paste the new code and the JavaScript console messages.

• ###### 13. Re: Help! help! How come Math pow( ) cannot work on this????

Hi, is still not working...

Script:

12*mth*(1-Math.pow(1.02,NY))/1-1.02

Console messages

ReferenceError: mth is not defined

1:AcroForm:total:Calculate

Hope to hear from you soon.

• ###### 14. Re: Help! help! How come Math pow( ) cannot work on this????

In order to access a field's value in JavaScript you can't just type the field's name. You have to use the appropriate methods and properties. So, for example, to get the value of a field called "mth" you need to use something like this:

var mth = this.getField("mth").value;

Then you can use the "mth" variable you've defined in the rest of your code.

• ###### 15. Re: Help! help! How come Math pow( ) cannot work on this????

It appears you are still trying to use the simplified field syntax for JavaScript. You cannot do this.

JavaScript is an object orientated language. That is, one must first link to an object like a field and then using that link access a property like the field's value or a method of the object.

One accesses a field object and then the properties or methods of that object.

You might want to look over the Acrobat JavaScript API Reference and the MDN JavaScript Reference as well as the various tutorials and videos posted by Adobe.

event.value = 12 * this.getField("mth").value * ( 1- Math.pow(1.02, this.getField(("NY").value)) / 1 - 1.02

• ###### 16. Re: Help! help! How come Math pow( ) cannot work on this????

By using your

event.value = 12 * this.getField("mth").value * ( 1- Math.pow(1.02, this.getField(("NY").value)) / 1 - 1.02

It appear message: SyntextError missing) in parenthetical line1: at line2

So i key in

event.value = 12 * this.getField("mth").value * ( 1- Math.pow(1.02, this.getField(("NY").value)) / 1 - 1.02);

it work but the total answer is not right it appear -1224

• ###### 17. Re: Help! help! How come Math pow( ) cannot work on this????

Why do you have (( instead of ( for the getField of field NY?

• ###### 18. Re: Help! help! How come Math pow( ) cannot work on this????

Hi,

tried a few ways but still cannot get the correct answer that i have, just wondering is there anything wrong with Math.pow part..

• ###### 19. Re: Help! help! How come Math pow( ) cannot work on this????

Looks wrong to me. Especially the two brackets. What are you trying to do there, and why is it different from the other getField? Since we didn't tell you to do that, presumably there was a reason that you did. Maybe it's a good reason. We can't know what is wrong without understanding why you did what you did.

• ###### 20. Re: Help! help! How come Math pow( ) cannot work on this????

There is a mistake in gkaiseril's code that you copied. In general, it's a bad idea to try to copy&paste code snippets you find online without knowing what you are doing. You've admitted that you don't know anything about JavaScript, and you will have to work on that. Otherwise, it's impossible to create a Javascript solution to a problem. We all make mistakes, and sometimes these mistakes make it into sample code. You need to be able to look at a line of code and realize that it's wrong by knowing basic Javascript syntax. There is no way around that. Do yourself a favor and get a good introduction to JavaScript and read the chapters about the basic syntax. You will feel a lot more comfortable afterwards - and more competent - when it comes to figuring out what's wrong with a script.

For beginners, it's usually very hard to wrap your head around an expression that has a lot of layers (e.g. function call inside a parameter to another function call). It's easier to define your variables upfront, and then use them when you need them. Let me give you an example in pseudo code:

You are using something like this:

```someVariable = someFunction(someConstant, someOtherFunction("a string").method);
```

Your code actually has a lot more parts to the expression. What you can do do make this a bit easier to swallow is break out the call to someOtherFunction() like this:

```var functionArgument = someOtherFunction("a string").method;
someVariable = someFunction(someConstant, functionArgument);
```

As you can see, your line #2 is now much shorter, and easier to understand.

You can define your two variables that you need for the two form field values upfront, and then use them when you need them by just referencing the variables:

```var mth = this.getField("mth").value;
var NY = this.getField("NY").value;
```

Once you get the basic formula working, you need to work on checking your input data and only calculate an output value is your input data is actually valid, but that's stuff for another day.