-3

I have a project I am working on and it needs to calculate mortgage calculations but I'm having trouble putting the formula into javascript.

the formula is:

M = P I(1 + I)^n /(1 + I )^n - 1

Any help is appreciated, thanks

P = loan princible

I = interest

N = Term

  • 2
    What have you researched and tried? Which parts can you do yourself and which parts did you have questions about? Hint, see [`Math.pow()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow). – jfriend00 Jul 25 '15 at 20:49
  • Being more specific let's us help you easier. What is `P`? A number I assume? – stalem Jul 25 '15 at 20:50
  • I have all my variables but I'm having trouble figuring out how to do the math with javascript. – Thomas Unise Jul 25 '15 at 20:51
  • possible duplicate of [Safe evaluation of arithmetic expressions in Javascript](http://stackoverflow.com/questions/5066824/safe-evaluation-of-arithmetic-expressions-in-javascript) – GillesC Jul 25 '15 at 20:52
  • PI is Pi, p*i im confused – Muhammad Umer Jul 25 '15 at 20:52
  • Possible duplicate: http://stackoverflow.com/questions/17101442/how-to-calculate-mortgage-in-javascript – jfriend00 Jul 25 '15 at 20:53

3 Answers3

2

Break it down into a sequence of steps.

  • Multiplication is as straightforward as it gets: I*(1+I)
  • Division is the same: I/(1+I)
  • To the power of n is denoted by: Math.pow(3, 5); //3 to the power of 5

Math.pow() might be the only thing you didn't know yet.


Unrelated but useful,

Wrap your formula into a function and you have a mortgage-calculation function

calculateMortgage(p,i,n) { result = //translate the formula in the way I indicated above return result; }

and call it like so:

var mortgage = calculateMortgage(300,3,2); // 'mortgage' variable will now hold the mortgage for L=300, I=3, N=2

Also, the formula you posted really doesn't make any sense - why is there a blank between P & I at the very beginning? Something's missing.

nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
0

Try this: Math.pow(p*i*(1+i),n)/Math.pow(1+i,n-1)

Math.pow(a,2) is same as a^2

if P is not supposed to be with numerator then this

p * (Math.pow(i*(1+i),n)/Math.pow(1+i,n-1))

or

p * (Math.pow((i+i*i),n)/Math.pow(1+i,n-1))

Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168
0
var M;
var P;
var I;

M = P*(Math.pow(I*(1+I),n)) / (Math.pow((1+I),n)-1);

Does this look right to you? I got the correctly styled formula from here.

Like what Nicholas above said, you can use functions to make it all the more easier.

var M;
function calculateMortgage(P, I, N){
    M = P*(Math.pow(I*(1+I),n)) / (Math.pow((1+I),n)-1);
    alert("Your mortgage is" + M);
}

And just call calculateMortgage(100, 100, 100); with your values for it to automatically give the answer to you.


Mingle Li
  • 1,322
  • 1
  • 15
  • 39