0

my code is suppose to calculate mortgage payments

var LA = 100000;
var RA=0.07;
var YA=30;

var R = ( RA / 12);
var r = (1 + R);
var Yr = (YA * 12);
var pay = (LA * Math.exp(r,Yr)*R)/(Math.pow(r,Yr)-1);

returns $224.12

which is wrong it needs to be $665.30 payment = [ LA * r^Yr * R ] / [ r ^Yr - 1]

For example:

30 year mortgage for $100,000 at 7% interest (0.07)

0.07 / 12 = 0.00583 (this is R)

30 * 12 = 360 (this is Yr)

1 + 0.00583 = 1.00583 (this is r)

payment = [ $100,000 * (1.00583)^360 * 0.00583 ] / [ (1.00583)^360 - 1 ]

Monthly Payments will be $665.30

any tips?

3
  • 4
    You cannot use square brackets in algebraic expressions. Just parentheses (round ones). Commented Oct 21, 2012 at 23:54
  • (LA * Math.exp(r,Yr)*R)/(Math.pow(r,Yr)-1); returns the same so i dont believe that is the issue but rather an standard that needs to be implemented Commented Oct 21, 2012 at 23:56
  • It is absolutely, positively true that square brackets should not be used as parts of algebraic computational expressions. They're syntactically valid because they construct arrays in JavaScript, and due to the way automatic type coercion happens you don't get errors, but they will absolutely not do what you think they do and you're asking for trouble. Commented Oct 22, 2012 at 0:14

1 Answer 1

3

Use the correct function: Math.pow and not Math.exp.

Also, although square brackets will work, it's only because JavaScript is casting the arrays to strings, and then to numbers. Use parentheses instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Is Math.exp silently ignoring the second argument?
Pretty much, yes. It's not an error to have too many arguments - nor is it one to have too few (although that ususally causes other errors)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.