function calculateMortgage() {
var loanAmount = parseFloat(document.getElementById("loanAmount").value);
var annualInterestRate = parseFloat(document.getElementById("annualInterestRate").value);
var loanTerm = parseFloat(document.getElementById("loanTerm").value);
var resultDiv = document.getElementById("mortgageResult");
var monthlyPaymentSpan = document.getElementById("monthlyPayment");
var totalInterestPaidSpan = document.getElementById("totalInterestPaid");
var totalRepaymentSpan = document.getElementById("totalRepayment");
// Clear previous results
monthlyPaymentSpan.innerHTML = ";
totalInterestPaidSpan.innerHTML = ";
totalRepaymentSpan.innerHTML = ";
if (isNaN(loanAmount) || isNaN(annualInterestRate) || isNaN(loanTerm) || loanAmount <= 0 || annualInterestRate < 0 || loanTerm <= 0) {
monthlyPaymentSpan.innerHTML = 'Please enter valid positive numbers for all fields.';
return;
}
var monthlyInterestRate = (annualInterestRate / 100) / 12;
var numberOfPayments = loanTerm * 12;
var monthlyPayment = 0;
if (monthlyInterestRate > 0) {
monthlyPayment = loanAmount * (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numberOfPayments)) / (Math.pow(1 + monthlyInterestRate, numberOfPayments) – 1);
} else {
monthlyPayment = loanAmount / numberOfPayments; // Simple division if interest rate is 0
}
var totalRepayment = monthlyPayment * numberOfPayments;
var totalInterestPaid = totalRepayment – loanAmount;
monthlyPaymentSpan.innerHTML = 'Estimated Monthly Payment: $' + monthlyPayment.toFixed(2) + '';
totalInterestPaidSpan.innerHTML = 'Total Interest Paid: $' + totalInterestPaid.toFixed(2) + '';
totalRepaymentSpan.innerHTML = 'Total Amount Repaid: $' + totalRepayment.toFixed(2) + '';
}
Understanding Your Mortgage Payment
A mortgage is a long-term loan used to purchase real estate. The monthly payment you make towards your mortgage typically consists of two main components: principal and interest. Over the life of the loan, the proportion of your payment going towards principal versus interest changes.
Key Components Explained:
Loan Amount (Principal): This is the actual amount of money you borrow from the lender to buy your home.
Annual Interest Rate: This is the yearly cost of borrowing the money, expressed as a percentage. Lenders use this to calculate the interest you'll owe. The actual interest paid is calculated on the outstanding balance of your loan.
Loan Term: This is the total duration of the loan, usually expressed in years. Common terms are 15, 20, or 30 years. A longer term means lower monthly payments but more interest paid overall.
How the Calculation Works:
The monthly mortgage payment is calculated using an amortization formula. This formula ensures that each payment gradually reduces the principal balance while also covering the interest accrued on the remaining balance. The formula is:
M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]
Where:
M = Your total monthly mortgage payment
P = Your mortgage's principal loan amount
i = Your monthly interest rate (annual rate divided by 12)
n = The total number of monthly payments over the loan's lifetime (loan term in years multiplied by 12)
Our calculator takes your inputs for Loan Amount, Annual Interest Rate, and Loan Term, and applies this formula to provide an estimated monthly payment, the total interest you'll pay over the loan's life, and the total amount repaid.
Example:
Let's say you're looking to buy a home and secure a mortgage with the following terms:
Loan Amount: $350,000
Annual Interest Rate: 6.25%
Loan Term: 30 years
Using the calculator:
The Estimated Monthly Payment would be approximately $2,154.46.
The Total Interest Paid over 30 years would be around $425,605.45.
The Total Amount Repaid would be approximately $775,605.45 ($350,000 principal + $425,605.45 interest).
This example illustrates how a significant portion of your early payments goes towards interest, and how the total interest paid can substantially increase the overall cost of your home over a long loan term.