function calculateCarLoan() {
const loanAmount = parseFloat(document.getElementById(‘loanAmount’).value);
const interestRate = parseFloat(document.getElementById(‘interestRate’).value) / 100 / 12;
const loanTerm = parseInt(document.getElementById(‘loanTerm’).value) * 12;
const downPayment = parseFloat(document.getElementById(‘downPayment’).value);
if (isNaN(loanAmount) || isNaN(interestRate) || isNaN(loanTerm) || isNaN(downPayment)) {
alert(‘Please fill in all fields with valid values.’);
return;
}
const principal = loanAmount – downPayment;
const monthlyPayment = (principal * interestRate) / (1 – Math.pow(1 + interestRate, -loanTerm));
const totalPaid = monthlyPayment * loanTerm;
const totalInterest = totalPaid – principal;
document.getElementById(‘monthlyPayment’).value = monthlyPayment.toFixed(2);
document.getElementById(‘totalInterest’).value = totalInterest.toFixed(2);
document.getElementById(‘totalCost’).value = totalPaid.toFixed(2);
}