Skip to main content
Participating Frequently
October 16, 2016
Question

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

  • October 16, 2016
  • 13 replies
  • 1617 views

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??

This topic has been closed for replies.

13 replies

Legend
October 16, 2016

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.

Participating Frequently
October 16, 2016

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..

Please advise.

Legend
October 16, 2016

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

Participating Frequently
October 16, 2016

Thanks for reply,

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

it there something right, please advise.

Karl Heinz  Kremer
Community Expert
Community Expert
October 16, 2016

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.

Participating Frequently
October 16, 2016

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

pdf file: http://adobe.ly/2dkAu3k

Hope to hear from you soon.

try67
Community Expert
Community Expert
October 16, 2016

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.

Legend
October 16, 2016

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

Participating Frequently
October 16, 2016

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.

Participating Frequently
October 16, 2016

Hi, Thank for replying

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

Legend
October 16, 2016

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)

Participating Frequently
October 16, 2016

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.

Legend
October 16, 2016

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.

Participating Frequently
October 16, 2016

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.

Awaiting for your help...please.

Regards

Thanks