Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript & Math 2

Status
Not open for further replies.

kizmar2

Programmer
May 25, 2004
164
US
How can I write the equivelant of this with Javascript?

Code:
                              J
         M  =  P  x ------------------------
                 
                      1  - ( 1 + J ) ^ -N

or another way to put it is:

Code:
M = P * ( J / (1 - (1 + J) ** -N))

I don't usually have to do math so I'm kinda lost here... I tried using the second part and it's not working.

KizMar
------------
 
One problem I see right away is the (1 + J). I tried something similar, and all it did was put them next to each other in a string. You might have to do:
(Number(1) + Number(J))
That is what I had to do for my dice roller.
 
Well, I'm coming up with some numbers now but the final number is way off... Thanks for the help though. I'll have to keep messing around with this to make sure my formula is being calculated right.

KizMar
------------
 
adninja has a good point about numbers. Unless something has been specifically converted to a number, it's best to assume it'll be handled as a string. Along the lines of that advice:

M = P * ( J / (Math.pow(1 - (1 + (J * 1)), -N));

Lee
 
There is no real reason why it has to be done all in one statement either. If you break the formula into several steps and store the intermediate values in variables it might be easier to read, and you can use alert to check the values of the intermediates and determine where the problems might be occurring.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Thanks for all your help. What I ended up using was:

Code:
P * ( J / (1 - Math.pow((1 + (J * 1)), -N)));

Which is trollacious's input with the extra ")" that was missing. ;)

By the way this is the formula that can be used to calculate a mortgage payment.

KizMar
------------
 
That missing ) must be why our bank are charging us stupid money to own a house [bigsmile]

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top