function calculate(){
// Look up the input and output elements in the document
var amount = document.getElementById("amount");
var apr = document.getElementById("apr");
var years = document.getElementById("years");
var zipcode = document.getElementById("zipcode");
var payment = document.getElementById("payment");
var total = document.getElementById("total");
var totalinterest = document.getElementById("totalinterest");
//Assume entries are valid
//Get principal value
var principal = parseFloat(amount.value);
//Get interest value convert from a percentage to a decimal
var interest = parseFloat(apr.value) / 100 / 12;
//Get payments value and convert payment period into years
var payments = parseFloat(years.value) * 12;
//complete the monthly payment figure
var x = Math.pow(1 + interest, payments); // Math.pow() computes powers
var monthly = (principal*x*interest)/(x-1);
//If the result is a finite number, the user's input was good and we hav meaningful results to display.
if (isFinite(monthly)){
// Fill in the output fields, rounding to 2 decimal places
payment.innert.HTML = monthly.toFixed(2);
total.innerHTML = (monthly * payments).toFixed(2);
totalinterest.innerHTML = ((monthly*payments)-principal).toFixed(2);
// Save the user's input so we can restore it the next time they visit
save(amount.value, apr.value, years.value, zipcode.value);
// Advertise: find and display local lenders but ingore network errors
try{ //catch any errors that occur within these curly braces
getLenders(amount.value, apr.value, years.value, zipcode.value);
}
catch(e) { /* and ignore these errors */ }
// Finally, chart loan balance, and interest and equity payments
chart(principal, interest, monthly, payments);
}else{
//Result...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.