Monthly
Bi-weekly
Weekly
Monthly Payment: $0.00
Total Interest Paid: $0.00
Total Loan Payment: $0.00
function calculateMortgage() {
const loanAmount = parseFloat(document.getElementById(“loanAmount”).value);
const loanTerm = parseFloat(document.getElementById(“loanTerm”).value);
const interestRate = parseFloat(document.getElementById(“interestRate”).value) / 100;
const paymentFrequency = document.getElementById(“paymentFrequency”).value;
if (isNaN(loanAmount) || isNaN(loanTerm) || isNaN(interestRate) || loanAmount <= 0 || loanTerm <= 0 || interestRate <= 0) {
alert("Please enter valid positive values for all fields.");
return;
}
const numberOfPayments = loanTerm * (paymentFrequency === "monthly" ? 12 : paymentFrequency === "bi-weekly" ? 26 : 52);
const monthlyRate = interestRate / 12;
const numerator = monthlyRate * Math.pow(1 + monthlyRate, numberOfPayments);
const denominator = Math.pow(1 + monthlyRate, numberOfPayments) – 1;
const monthlyPayment = loanAmount * numerator / denominator;
const totalPayment = monthlyPayment * numberOfPayments;
const totalInterest = totalPayment – loanAmount;
document.getElementById("monthlyPayment").textContent = `$${monthlyPayment.toFixed(2)}`;
document.getElementById("totalInterest").textContent = `$${totalInterest.toFixed(2)}`;
document.getElementById("totalPayment").textContent = `$${totalPayment.toFixed(2)}`;
}