Auto loan calculator website widget

70 Views Asked by At

I was going to try my luck on StackOverflow, but I have a feeling my issue here is on order of operations.

I'm using the loan calculation found here to build a loan calculator for a clients website.

http://teachertech.rice.edu/Participants/bchristo/lessons/carpaymt.html

      P ( r / 12 )
      -------------------------
      (1 - ( 1 + r / 12 ) ^-m  )

$$ \frac{P (r/12)}{1 - (1+ r/12)^{-m}} $$

As set up now, I'm finding the sum of the top, sum of the bottom, then dividing.

Here's my fiddle if it makes sense to anyone.

https://jsfiddle.net/9dbhcLrq/

(top)

  1. rate/12
  2. loanAmount * (sum of step 1) = topPart

(bottom)

  1. rate / 12
  2. 1 + (sum of step 1)
  3. (sum of step 2) ^ -months
  4. 1 - (sum of step 3)

Then top/bottom.

My answer differs from the same variables placed in google's auto loan calculator.

I cannot find a problem with my javascript. It must be in the order of ops.

What am I doing wrong?

1

There are 1 best solutions below

0
On

Your code:

    var firstStep = apr / 12;
    var secondStep = loanAmount * firstStep;
    var top = secondStep;

    var bottomStep1 = apr / 12;
    var bottomStep2 = 1 + bottomStep1;
    var bottomStep3 = Math.pow(bottomStep2, -months);
    var bottomStep4 = 1 - bottomStep3
    var bottom = bottomStep4;

    var compAnswer = top / bottom;

Can be reduced to

var rpm = apr / 12.0;
var numerator = loanAmount * rpm;
var denominator = 1.0 - Math.pow(1.0 + rpm, -months);
var compAnswer = numerator / denominator;

but it should return the same number.

You should compare your results with that loan calculator from teachertech.

The google calculator might use a different calculation formula.