document.addEventListener('DOMContentLoaded', function () { const loanAmountInput = document.getElementById('loanAmount'); const interestRateInput = document.getElementById('interestRate'); const loanTermInput = document.getElementById('loanTerm'); const monthlyPaymentDisplay = document.getElementById('monthlyPayment'); function calculateMonthlyPayment() { const principal = parseFloat(loanAmountInput.value); const annualInterestRate = parseFloat(interestRateInput.value); const years = parseFloat(loanTermInput.value); if (isNaN(principal) || isNaN(annualInterestRate) || isNaN(years) || principal <= 0 || annualInterestRate <= 0 || years <= 0) { monthlyPaymentDisplay.textContent = '$0.00'; return; } const monthlyInterestRate = annualInterestRate / 100 / 12; const numberOfPayments = years * 12; const numerator = monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numberOfPayments); const denominator = Math.pow(1 + monthlyInterestRate, numberOfPayments) - 1; const monthlyPayment = principal * (numerator / denominator); if (isFinite(monthlyPayment)) { monthlyPaymentDisplay.textContent = `$${monthlyPayment.toFixed(2)}`; } else { monthlyPaymentDisplay.textContent = '$0.00'; } } loanAmountInput.addEventListener('input', calculateMonthlyPayment); interestRateInput.addEventListener('input', calculateMonthlyPayment); loanTermInput.addEventListener('input', calculateMonthlyPayment); calculateMonthlyPayment(); });