function calculateMortgage() {
// 1. Get Input Values
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);
// 2. Validation
if (isNaN(homePrice) || isNaN(downPayment) || isNaN(interestRate) || isNaN(loanTerm) || isNaN(propertyTax) || isNaN(homeInsurance)) {
alert("Please enter valid numbers in all fields.");
return;
}
if (homePrice <= 0 || loanTerm <= 0) {
alert("Home Price and Loan Term must be greater than zero.");
return;
}
// 3. Calculation Logic
var principal = homePrice – downPayment;
var monthlyRate = (interestRate / 100) / 12;
var numberOfPayments = loanTerm * 12;
// Calculate Principal & Interest (P&I)
var monthlyPI = 0;
if (monthlyRate === 0) {
monthlyPI = principal / numberOfPayments;
} else {
// Formula: M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]
monthlyPI = principal * (monthlyRate * Math.pow(1 + monthlyRate, numberOfPayments)) / (Math.pow(1 + monthlyRate, numberOfPayments) – 1);
}
// Calculate Monthly Tax and Insurance
var monthlyTaxVal = propertyTax / 12;
var monthlyInsVal = homeInsurance / 12;
// Calculate Total
var totalMonthlyPayment = monthlyPI + monthlyTaxVal + monthlyInsVal;
// 4. Formatting Output
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2
});
// 5. Update DOM
document.getElementById('displayPI').innerText = formatter.format(monthlyPI);
document.getElementById('displayTax').innerText = formatter.format(monthlyTaxVal);
document.getElementById('displayInsurance').innerText = formatter.format(monthlyInsVal);
document.getElementById('displayTotal').innerText = formatter.format(totalMonthlyPayment);
// Show results container
document.getElementById('resultsSection').style.display = 'block';
}
Understanding Your Mortgage Calculation
Calculating your monthly mortgage payment is the first critical step in the home buying process. This calculator breaks down the costs associated with owning a home, going beyond just the loan repayment to include essential recurring costs like taxes and insurance.
Key Components of Your Monthly Payment
Principal: The portion of your payment that goes toward paying down the original amount you borrowed. As you make payments over time, the principal portion increases.
Interest: The cost of borrowing money from your lender. In the early years of your loan, a larger percentage of your payment goes toward interest.
Property Taxes: An annual tax levied by your local government based on your property's value. This is typically divided by 12 and collected monthly by your lender into an escrow account.
Homeowners Insurance: Insurance that protects your home against damages. Like property taxes, the annual premium is usually split into monthly installments included in your mortgage payment.
How the Mortgage Formula Works
The core calculation for the Principal and Interest (P&I) portion of your mortgage uses the standard amortization formula:
M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]
Where:
M = Total monthly payment
P = Principal loan amount (Home Price minus Down Payment)
i = Monthly interest rate (Annual Rate divided by 12)
n = Number of payments (Loan term in years multiplied by 12)
Why Your Interest Rate Matters
Even a small difference in interest rates can significantly impact your monthly payment and the total amount you pay over the life of the loan. For example, on a $300,000 loan, a 1% difference in interest rate can change your monthly payment by over $180 and save (or cost) you more than $60,000 over 30 years.