function calculateMortgage() {
// Get Input Values
var homePrice = parseFloat(document.getElementById('homePrice').value) || 0;
var downPayment = parseFloat(document.getElementById('downPayment').value) || 0;
var loanTermYears = parseInt(document.getElementById('loanTerm').value) || 30;
var annualRate = parseFloat(document.getElementById('interestRate').value) || 0;
var annualTax = parseFloat(document.getElementById('propertyTax').value) || 0;
var annualIns = parseFloat(document.getElementById('homeInsurance').value) || 0;
var monthlyHOA = parseFloat(document.getElementById('hoaFees').value) || 0;
// Validations
if (homePrice <= 0) {
alert("Please enter a valid Home Price.");
return;
}
// 1. Calculate Loan Principal
var principal = homePrice – downPayment;
if (principal < 0) principal = 0;
// 2. Calculate Monthly Principal & Interest (P&I)
// Formula: M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1 ]
var monthlyRate = (annualRate / 100) / 12;
var numberOfPayments = loanTermYears * 12;
var monthlyPI = 0;
if (annualRate === 0) {
monthlyPI = principal / numberOfPayments;
} else {
var numerator = monthlyRate * Math.pow(1 + monthlyRate, numberOfPayments);
var denominator = Math.pow(1 + monthlyRate, numberOfPayments) – 1;
monthlyPI = principal * (numerator / denominator);
}
// 3. Calculate Monthly Tax and Insurance
var monthlyTax = annualTax / 12;
var monthlyIns = annualIns / 12;
// 4. Total Monthly Payment
var totalMonthly = monthlyPI + monthlyTax + monthlyIns + monthlyHOA;
// 5. Total Interest Over Life of Loan
var totalCost = monthlyPI * numberOfPayments;
var totalInterest = totalCost – principal;
// 6. Formatting Function
function formatMoney(num) {
return '$' + num.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
}
// 7. Update DOM
document.getElementById('totalMonthlyPayment').innerHTML = formatMoney(totalMonthly);
document.getElementById('piAmount').innerHTML = formatMoney(monthlyPI);
document.getElementById('taxAmount').innerHTML = formatMoney(monthlyTax);
document.getElementById('insAmount').innerHTML = formatMoney(monthlyIns);
document.getElementById('hoaAmount').innerHTML = formatMoney(monthlyHOA);
document.getElementById('loanPrincipal').innerHTML = formatMoney(principal);
document.getElementById('totalInterest').innerHTML = formatMoney(totalInterest);
// Show Results
document.getElementById('resultsArea').style.display = 'block';
}
Understanding Your Mortgage Payment
Calculating your monthly mortgage payment is the first step in determining "how much house" you can afford. While the sticker price of a home gives you a general idea, your actual monthly obligation is composed of several factors, often referred to as PITI (Principal, Interest, Taxes, and Insurance).
What goes into your monthly payment?
Principal: The portion of your payment that goes directly toward paying down the loan balance. In the early years of a mortgage, this amount is small, but it grows over time.
Interest: The cost of borrowing money from your lender. Higher interest rates significantly increase your monthly payment and the total cost of the loan.
Property Taxes: Taxes assessed by your local government based on the value of the property. These are usually bundled into your mortgage payment and held in escrow.
Homeowners Insurance: Protects your home against damage. Like taxes, this is often paid monthly into an escrow account.
HOA Fees: If you buy a condo or a home in a planned community, you may pay Homeowners Association fees. These are rarely included in the loan but must be budgeted for.
How Interest Rates Affect Affordability
Even a small change in interest rates can have a massive impact on your purchasing power. For example, on a $300,000 loan, a 1% increase in interest rate can raise the monthly payment by over $150 and cost tens of thousands of dollars in extra interest over the life of a 30-year loan.
The Importance of the Down Payment
Your down payment reduces the principal amount you need to borrow. A larger down payment (typically 20% or more) can help you avoid Private Mortgage Insurance (PMI), lower your monthly payment, and secure a better interest rate from lenders.
15-Year vs. 30-Year Mortgages
While a 30-year mortgage offers lower monthly payments, a 15-year mortgage will save you a significant amount in interest over the life of the loan. Use the calculator above to compare the total interest paid between the two terms to see the long-term financial difference.