Changed around line 1
+ function calculateLoan(amount, duration, rate) {
+ const monthlyRate = rate / 100 / 12;
+ const numberOfPayments = duration * 12;
+
+ const monthlyPayment =
+ (amount * monthlyRate) /
+ (1 - Math.pow(1 + monthlyRate, -numberOfPayments));
+
+ const totalInterest = (monthlyPayment * numberOfPayments) - amount;
+
+ return {
+ monthly: monthlyPayment.toFixed(2),
+ totalInterest: totalInterest.toFixed(2)
+ };
+ }
+
+ function updateResults() {
+ const amount = parseFloat(document.getElementById('amount').value);
+ const duration = parseFloat(document.getElementById('duration').value);
+ const rate = parseFloat(document.getElementById('rate').value);
+
+ const result = calculateLoan(amount, duration, rate);
+
+ document.getElementById('monthlyPayment').textContent = result.monthly;
+ document.getElementById('totalInterest').textContent = result.totalInterest;
+
+ updateComparisonTable(amount, duration);
+ }
+
+ function updateComparisonTable(amount, duration) {
+ const tableBody = document.querySelector('#rateComparison tbody');
+ tableBody.innerHTML = '';
+
+ const rates = [2.5, 3.0, 3.5, 4.0, 4.5];
+
+ rates.forEach(rate => {
+ const result = calculateLoan(amount, duration, rate);
+
+ const row = document.createElement('tr');
+ row.innerHTML = `
+
${rate}% | +
${result.monthly} € | +
${result.totalInterest} € | + `;
+
+ tableBody.appendChild(row);
+ });
+ }
+
+ document.getElementById('calculate').addEventListener('click', updateResults);
+
+ // Initial calculation
+ updateResults();