# Javascript mortgage calculator formula and code example (2)

//Javascript mortgage calculator formula and code example (2)

## Simple javascript mortgage calculator Example:

The javascript mortgage calculator will need the following input values:

PR (present value of the mortgage loan or principal amount)
IN (annual interest rate of the mortgage loan)
PE (number of periods of the mortgage loan in years, or loan term in years)

Just copy and paste the following JavaScript code (in blue) in a html page, after the <body> tag, but before the ending </body> tag.

<script language="JavaScript" type="text/javascript">
<!– hide the mortgage calculator formula from non JavaScript browsers
function find_payment(PR, IN, PE) {
var PAY = (PR * IN) / (1 – Math.pow(1 + IN, -PE));
return PAY
}
var principal = 200000
var interest = 0.09
var term = 30
var monthly_payment = find_payment(principal, interest / 12, term * 12)
alert("Amount of the loan:\t\$" + principal + "\n" +
"Annual interest rate:\t" + interest * 100 + "%\n" +
"Term of the mortgage loan:\t" + term + " years\n\n" +
"Monthly payment:\t\$" + monthly_payment)
//–> End of hidden JavaScript for Browsers not supporting it
</script>

The Javascript function find_payment() does the heavy javascript mortgage calculator calculations here. It takes as arguments the three terms required by the expression: PR, IN, and PE (mortgage principal amount, mortgage interest rate and mortgage periods in years).

The result of the find_payment function code is stored in the Javascript PAY variable, which is then transferred to the monthly_payment variable.

Finally, the result is shown in an alert box.

This Javascript mortgage calculator script example uses fixed values: principal, interest and term (hand coded in the above example). Those fixed values are passed to the find_payment function.

Before the javascript mortgage calculator find_payment function does it’s calculations, the interest rate is divided by 12 and the term is multiplied by 12. This is necessary because those values were annual values, not monthly.

To change the results, simply replace one of the hard coded variables: principal, interest or the term.

This is part 2 of the post, read part 1 here.

By | 2017-01-24T12:19:29+00:00 April 18th|Categories: Mortgage formula|Comments Off on Javascript mortgage calculator formula and code example (2)