## Mortgage Calculator With Amortization

Introduction | Demonstration Calculator | Source Code

## How The Mortgage Calculator with Amortization Works

This calculator uses the same mathematical functions as our Simple Mortgage Payment Calculator, but creates a loop, going through all the months and calculating the interest to principal ratios for each payment as you pay down the principal each month.

The amortization display is very simple. The output for each month is generated by one line of code…

``````
factsout += '<b>Year ' + (inyear + 1) + ' Month ' + (inmonth +1) + ':</b><br>   Principal Paid: \$' + princpaid + '<br>   Interest Paid: \$' + intpaid + '<br>   Principal Remaining: \$' + loan + '<br>';
``````

If you’re familiar with HTML and just have a basic knowledge of JavaScript, you can edit that to add all sorts of formatting goodness to your output.

Introduction | Demonstration Calculator | Source Code

## Demonstration Calculator

Loan Amount:
Loan Term: years
APR Interest:

Payment:

Introduction | Demonstration Calculator | Source Code

## Mortgage Calculator with Amortization Source Code

This calculator is licensed to you under Creative Commons Attribution 3.0. If you would like to use the source code on your site or create a derivative work from it, you must include the following HTML on the page where the calculator appears. We’re giving you this code for free with a legal license to use it. All you have to do is credit us in the manner we specify.

Required Credit HTML

INSTALLATION

Just cut and paste the Javascript code and HTML form below into your page. If you want to customize the appearance of the form, feel free. Just make sure the input name and form id/name values are not changed.

``````

<script type="text/javascript">

var amortsub = false;

function calcLoan() {

var formvals = getFormVal();

var years = formvals;
var months = years * 12;

var loan = formvals;
var apr = formvals;

var mpr = apr / 1200;
var nfactor = 0 - months;
var mofactor = Math.pow((1 + mpr), nfactor);
var bofactor = 1 - mofactor;
var tofactor = mpr / bofactor;

var payment = loan * tofactor;
var reducto = Math.round(payment*100)/100;

document.wcbubba.payment.value = "\$"+reducto;

if (amortsub) showAm();

}

function showAm(){

amortsub = true;

formvals = getFormVal();

var years = formvals;
var months = years * 12;

var loan = formvals;
var apr = formvals;

var mpr = apr / 1200;
var nfactor = 0 - months;
var mofactor = Math.pow((1 + mpr), nfactor);
var bofactor = 1 - mofactor;
var tofactor = mpr / bofactor;

var payment = loan * tofactor;
var reducto = Math.round(payment*100)/100;

document.wcbubba.payment.value = "\$"+reducto;

// NOW WE CALCULATE THE AMORTIZATION

var intpaid = 0;
var princpaid = 0;
var factsout = "";
var inyear = 0;
var inmonth = 0;

document.getElementById('amortsub').innerHTML = '<b style="font-size:19px;">MONTHLY AMORTIZATION</b><br><br>';

for(var i=0;i<months;i++){
intpaid = Math.round((mpr * loan) *100)/100;
princpaid = Math.round((reducto - intpaid)*100)/100;
loan = Math.round((loan - princpaid)*100)/100;

inyear = parseInt(i/12);
inmonth = i - (inyear * 12);

factsout += '<b>Year ' + (inyear + 1) + ' Month ' + (inmonth +1) + ':</b><br>   Principal Paid: \$' + princpaid + '<br>   Interest Paid: \$' + intpaid + '<br>   Principal Remaining: \$' + loan + '<br>';

}
document.getElementById('amortsub').innerHTML += factsout;

flush();

}

function getFormVal(){

var years = parseInt(document.wcbubba.term.value);
var loan = parseInt(document.wcbubba.loan.value);
var apr = parseFloat(document.wcbubba.apr.value);

if((years <= 0)||(isNaN(years))) years = 1;
if((apr <= 0)||(isNaN(apr))) apr = 1;
if((loan <= 0)||(isNaN(loan))) loan = 1;

var mike = new Array(years,apr,loan);

return mike;
}

</script>

<form id="wcbubba" name="wcbubba" action="javascript:void();">
Loan Amount: <input type=text name="loan" size=8><br>
Loan Term: <input type=text name="term" size=8> years<br>
APR Interest: <input type=text name="apr" size=8><br><ber>

<input type="submit" value="Calculate Payment" onClick="calcLoan();"> <b>Payment:</b> <input type=text name = "payment" size=9><br>
<input type="submit" value="Show Amortization" onClick="showAm();">
</form><br><br>

<div id="amortsub">

</div>
``````