Mortgage calculators for website programmers
Canadian JavaScript mortgage calculator codeCalculateur de prêt en français 

1 Stop Mortgage Calculator - Calculate your Mortgage payments in Seconds!
Mortgage Calculator Code - Part 2, Programming the JavaScript mortgage formula

Mortgage calculator code

Part 2
How to program the mortgage formula in JavaScript

In part 1 of this Mortgage calculator code series, we learned that the heart of the calculator is the mortgage formula:

Pa = PrValue x IntRate / (1 – (1 + IntRate)–Period)

The Pa variable represents the payment, the present value of the loan is represented by the PrValue variable, the interest rate by IntRate and Period represents the number of years you are borrowing the money.

 

How do we program the mortgage formula?

Basic mortgage formula

For an international or a US mortgage the basic formula is the one I mentioned earlier:

Pa = PrValue x IntRate / (1 – (1 + IntRate)–Period)

In JavaScript, the code is:

Pa = (PrValue * IntRate) / (1 - Math.pow(1 + IntRate, -Period))

The trick here, is to use the Math.pow(x,y) function.

In the JavaScript Math.pow (x, y) function, x is the base number and y is the exponent (i.e. the power to which the base number is raised). For example, Math.pow(2, 3) raises 2 to the power of 3, which gives 8.

This is where everything seems to get complicated. The formula is for annual payments... and usually we are looking for monthly payments. We could simply modify the formula to do that, but ... what if we want to know the bi-weekly payments or weekly payments? And what about accelerated bi-weekly payments?

What we need to find out is the real interest rate for our payment cycle. For example, if it's monthly payments, our real interest is (Annual Interest Rate / 12), if it's accelerated by-weekly payments, our real interest is (Annual Interest Rate / 26), if it's non-accelerated bi-weekly payment, the real interest rate is
(Annual Interest Rate / 12) / 2
or (Annual Interest Rate / 24), etc.

Our new JavaScript mortgage code is now 2 formulas. The first one finds the real interest rate and the second one finds the periodic payment.

Here's the mortgage calculator code in JavaScript form:

IntRate = AnnualIntRate / PPay
where PPay is the number of payments per year

Pa = (PrValue * IntRate) / (1 - Math.pow (1 + IntRate, - (Period) )

But our formula is now incorrect! See why in part 3.

Next topic: Mortgage calculator code and interest calculations.


To difficult to do? Just click on this link to get ready-to-use JavaScript mortgage calculators that you can install on your website or for offline use on your computer!


Mortgage Calculators Products Services Help About Us Site Map

© Copyright (C) 2003-2010 by Dominique Peladeau • 1stop-mortgagecalculator.com.
All Rights Reserved
886 de L'Epee ave., Outremont, QC, H3V 3V3 • (514) 571-9747 (9-5 EST)
Contact us

1 stop mortgage calculator code logo