function calculateAutoLoan() {
const loanAmount = parseFloat(document.getElementById(“loanAmount”).value);
const interestRate = parseFloat(document.getElementById(“interestRate”).value) / 100;
const loanTerm = parseInt(document.getElementById(“loanTerm”).value);
const earlyPayment = parseFloat(document.getElementById(“earlyPayment”).value);
if (isNaN(loanAmount) || isNaN(interestRate) || isNaN(loanTerm) || isNaN(earlyPayment)) {
alert(“Please enter valid values for all fields.”);
return;
}
const monthlyInterestRate = interestRate / 12;
const numberOfPayments = loanTerm * 12;
// Monthly payment calculation using the loan formula
const monthlyPayment = loanAmount * monthlyInterestRate / (1 – Math.pow(1 + monthlyInterestRate, -numberOfPayments));
// Early payoff savings calculation
const totalPayments = monthlyPayment * numberOfPayments;
const earlyPaidTotal = totalPayments – (earlyPayment * numberOfPayments);
document.getElementById(“totalPayment”).value = (totalPayments – earlyPaidTotal).toFixed(2);
document.getElementById(“savings”).value = earlyPaidTotal.toFixed(2);
}