function calculateMortgage() {
// Use 'var' as requested
var homePrice = parseFloat(document.getElementById('homePrice').value);
var downPayment = parseFloat(document.getElementById('downPayment').value);
var interestRate = parseFloat(document.getElementById('interestRate').value);
var loanTerm = parseFloat(document.getElementById('loanTerm').value);
var propertyTax = parseFloat(document.getElementById('propertyTax').value);
var homeInsurance = parseFloat(document.getElementById('homeInsurance').value);
var errorMsg = document.getElementById('errorMsg');
var resultsArea = document.getElementById('resultsArea');
// Validation
if (isNaN(homePrice) || isNaN(downPayment) || isNaN(interestRate) || isNaN(loanTerm) || isNaN(propertyTax) || isNaN(homeInsurance)) {
errorMsg.style.display = 'block';
resultsArea.style.display = 'none';
return;
}
// Logical validation
if (downPayment >= homePrice) {
errorMsg.innerText = "Down payment cannot be greater than or equal to home price.";
errorMsg.style.display = 'block';
resultsArea.style.display = 'none';
return;
}
errorMsg.style.display = 'none';
// Calculation Logic
var principal = homePrice – downPayment;
var monthlyRate = (interestRate / 100) / 12;
var numberOfPayments = loanTerm * 12;
// Amortization Formula: M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1 ]
var monthlyPI = 0;
if (interestRate === 0) {
monthlyPI = principal / numberOfPayments;
} else {
monthlyPI = principal * (monthlyRate * Math.pow(1 + monthlyRate, numberOfPayments)) / (Math.pow(1 + monthlyRate, numberOfPayments) – 1);
}
var monthlyTax = propertyTax / 12;
var monthlyIns = homeInsurance / 12;
var totalMonthly = monthlyPI + monthlyTax + monthlyIns;
var totalInterest = (monthlyPI * numberOfPayments) – principal;
var totalCost = principal + totalInterest;
// Payoff Date Estimate
var today = new Date();
var payoffYear = today.getFullYear() + loanTerm;
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var payoffDateStr = monthNames[today.getMonth()] + " " + payoffYear;
// Updating DOM
document.getElementById('totalMonthlyDisplay').innerText = "$" + totalMonthly.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2});
document.getElementById('piDisplay').innerText = "$" + monthlyPI.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2});
document.getElementById('taxDisplay').innerText = "$" + monthlyTax.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2});
document.getElementById('insDisplay').innerText = "$" + monthlyIns.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2});
document.getElementById('totalInterestDisplay').innerText = "$" + totalInterest.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2});
document.getElementById('totalCostDisplay').innerText = "$" + totalCost.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2});
document.getElementById('payoffDateDisplay').innerText = payoffDateStr;
resultsArea.style.display = 'block';
}
Understanding Your Mortgage Payment
Buying a home is one of the most significant financial decisions you will ever make. Using a comprehensive mortgage calculator is essential to understanding not just the purchase price, but the actual monthly impact on your budget. This tool helps break down the costs associated with homeownership, known collectively as PITI.
What is PITI?
PITI stands for Principal, Interest, Taxes, and Insurance. These are the four main components that make up your monthly mortgage bill:
Principal: The portion of your payment that goes toward paying down the loan balance ($350,000 home price – down payment).
Interest: The cost of borrowing money from the lender. In the early years of a mortgage, a large percentage of your payment goes toward interest.
Taxes: Property taxes assessed by your local government, typically collected by your lender in an escrow account.
Insurance: Homeowners insurance protects your property against damage and liability. Lenders require this to protect their asset.
How Interest Rates Affect Affordability
Even a small difference in interest rates can have a massive impact on your total loan cost. For example, on a $300,000 loan, the difference between a 6% and a 7% interest rate can add hundreds of dollars to your monthly payment and tens of thousands of dollars in interest over the life of a 30-year loan.
Using This Calculator for Budgeting
When planning your home purchase, don't just look at the sticker price. Enter your estimated property tax and insurance costs into the calculator above. These "hidden" costs can sometimes push a monthly payment out of your comfort zone. A good rule of thumb is to keep your total housing payment (PITI) under 28% of your gross monthly income.
Common Mortgage Terms Explained
Loan Term: The lifespan of the loan. The most common terms are 15 and 30 years. A 15-year term usually has a lower interest rate and pays off debt faster, but comes with a significantly higher monthly payment compared to a 30-year term.
Down Payment: The upfront cash you pay toward the home. A higher down payment reduces your loan amount, monthly payment, and total interest paid. Typically, putting down less than 20% may trigger Private Mortgage Insurance (PMI), an extra cost not included in the basic calculation above unless factored into insurance inputs.