const balanceInput = document.getElementById('balance-1'); const interestRateInput = document.getElementById('interest-rate-1'); const paymentInput = document.getElementById('payment-1'); const calculateButton = document.getElementById('calculate-1'); const payoffTimeOutput = document.getElementById('payoff-time-1'); const totalInterestOutput = document.getElementById('total-interest-1'); calculateButton.addEventListener('click', function() { const balance = parseFloat(balanceInput.value); const interestRate = parseFloat(interestRateInput.value); const payment = parseFloat(paymentInput.value); if (isNaN(balance) || isNaN(interestRate) || isNaN(payment)) { alert('Please enter valid numbers for all fields.'); return; } const monthlyInterestRate = interestRate / 100 / 12; let remainingBalance = balance; let totalInterest = 0; let months = 0; while (remainingBalance > 0) { const interest = remainingBalance * monthlyInterestRate; totalInterest += interest; remainingBalance += interest; remainingBalance -= payment; if (totalInterest > 600) { alert('Payoff time is over 50 years. Please adjust your payment amount or interest rate.'); payoffTimeOutput.textContent = ''; totalInterestOutput.textContent = ''; return; } months++; } const years = Math.floor(months / 12); const remainingMonths = months % 12; const payoffTime = years + ' years' + (remainingMonths > 0 ? ' and ' + remainingMonths + ' months' : ''); payoffTimeOutput.textContent = payoffTime; totalInterestOutput.textContent = '$' + totalInterest.toFixed(2); // Display results document.querySelector('.result-1').style.display = 'block'; });