New Monthly Payment: $0.00
Total Savings: $0.00
function calculateRefinance() {
const loanAmount = parseFloat(document.getElementById(“loanAmount”).value);
const interestRate = parseFloat(document.getElementById(“interestRate”).value) / 100;
const loanTerm = parseInt(document.getElementById(“loanTerm”).value);
const currentMonthlyPayment = parseFloat(document.getElementById(“currentMonthlyPayment”).value);
const newInterestRate = parseFloat(document.getElementById(“newInterestRate”).value) / 100;
const refinanceLoanTerm = parseInt(document.getElementById(“refinanceLoanTerm”).value);
// Formula to calculate monthly payments for a loan
function calculateMonthlyPayment(principal, rate, term) {
const monthlyRate = rate / 12;
const numberOfPayments = term * 12;
return principal * monthlyRate / (1 – Math.pow(1 + monthlyRate, -numberOfPayments));
}
if (isNaN(loanAmount) || isNaN(interestRate) || isNaN(loanTerm) || isNaN(currentMonthlyPayment) || isNaN(newInterestRate) || isNaN(refinanceLoanTerm)) {
alert(“Please fill in all fields correctly.”);
return;
}
const newMonthlyPayment = calculateMonthlyPayment(loanAmount, newInterestRate, refinanceLoanTerm);
const savings = (currentMonthlyPayment * loanTerm * 12) – (newMonthlyPayment * refinanceLoanTerm * 12);
document.getElementById(“monthlyPaymentResult”).textContent = `New Monthly Payment: $${newMonthlyPayment.toFixed(2)}`;
document.getElementById(“totalSavings”).textContent = `Total Savings: $${savings.toFixed(2)}`;
}