# How long until I pay off my Mortgage? Mortgage calculator using Javascript

Hi! In this post, I will be doing an application of an annuity. Remember to form the previous posts, here and here, that an annuity is an investment that pays a series of future payments and from those payments, I want to know how much is a reasonable price to pay for it. A mortgage, if you the mortgage borrower pay a constant stream of cash, is an annuity for the mortgage lender, except here the value of the annuity is the difference between the cost of the home and the down payment and what we need to figure out now is how many payments we’d need to make.

For the Calculator use the Link bellow:

https://jsfiddle.net/Zerkon/su01bz26/1/show/

Why is a Mortgage an annuity?

If you think about it an annuity is an investment that pays a series of future payments. If you were to take out a mortgage to buy a home and then you’d pay a constant amount every month, this is an annuity, not for you but the lender.

This case the price of the annuity is the difference between your house and the down payment so there are two things you can calculate for. First, you can find how long it would take to pay off the mortgage if you have a constant payment each month. Second, you can find how much you have to pay each month if you want it paid off in a specific time limit.

In this post I will be calculating the time, in a future post, I will be calculating the size of the payments.

remember that the formula for pricing an annuity is:

$p = \frac{c}{r} (1- \frac{1}{(1+r)^t})$

Here p will be the difference between the cost of your house and the down payment, r is the monthly interest rate of the mortgage loan and c is the monthly payments. We now solve for t which gives us:

$t = \frac{ln(c)-ln(c-pr)}{ln(1+r)}$

I’ll quickly show you the HTML section of the code and then go over to the heavy lifting using Javascript.

HTML

Here is the layout for the calculator

<h2>Simple Mortgage Calculator (time)</h2>
<table style="width:100%">
<tr>
<td width="150px"> Cost of property</td>
<td>
<input id="price"></td>
</tr>
<tr>
<td> Down Payment</td>
<td>
<input id="down"></td>
</tr>
<tr>
<td> Interest Rate (%)</td>
<td>
<input id="interest"></td>
</tr>
<tr>
<td> Monthly Payments</td>
<td>
<input id="payments"></td>
</tr>
<tr>
<td></td>
<td>
<button type="button" onclick="myFunction()">Submit</button></td>
</tr>
</table>



Javascript

With Javascript log1P(x) = ln(1+x) so thus I had to subtract 1 from every value that was placed in log1P(x) to make sure that I got an accurate value.


var temp2 = Math.log1p(payments - 1) - Math.log1p(payments - temp1 - 1);



Another consideration I made was the fact that if you were to make monthly payments that were smaller than the interest, the calculator would warn you that it would be impossible to pay off the mortgage and would give you a minimum value for payments.

Also, note that although the user is entering in the stated rate of interest, it is the effective rate of interest that is used. This is calculated by dividing the rate by 365 adding 1 and raising it to the power of 365. I then take the 12th root to find the rate for each month.

Here’s the full code:


function myFunction() {
var text, text2;
var time;

var property = document.getElementById("price").value;
var down_payment = document.getElementById("down").value;
var interest = document.getElementById("interest").value;
var payments = document.getElementById("payments").value;

if (isNaN(property) || isNaN(down_payment) || isNaN(interest) || isNaN(payments)) {
//checks to see if the input is valid
text = "Input not valid";
} else {
if (property < 0 || down_payment < 0 || interest < 0 || payments < 0) {
//checks to see if the values are positive
text = "Input not valid";
} else {
//do computations here

//compute the actual monthly rate
var rate = Math.pow(1 + interest / 36500, 365);
rate = Math.pow(rate, .0833) - 1;

//calculate intermittent values
var temp1 = (property - down_payment) * rate;

//if the interest from the mortgage is larger then the payments then the debt will never be repaied
if (temp1 < payments) {
//calculates the time it takes to pay off the mortgage
var temp2 = Math.log1p(payments - 1) - Math.log1p(payments - temp1 - 1);
temp2 = temp2 / Math.log1p(rate);
temp2 = Math.ceil(temp2);

temp3 = temp2 / 12;
temp3 = Math.floor(temp3);

temp4 = temp2 - temp3 * 12;

text = "you will be able to pay off your mortgage in ";

if (temp3 == 1) {
text = text + "one year ";
} else if (temp3 > 1) {
text = text + temp2 + " years ";
}

if (temp4 == 1) {
if (temp3 > 0) text + "and ";
text = text + "one month";
} else if (temp4 > 1) {
if (temp3 > 0) text + "and ";
text = text + temp4 + " months";
}
} else {
text = "Payments are too small, mortgate will not be paid off. Payments must be more then \$" + temp1.toFixed(3) + " per month";
}
}
}