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 totalCost = monthlyPayment * numberOfPayments;
+ return { monthlyPayment, totalCost };
+ }
+
+ function formatCurrency(amount) {
+ return new Intl.NumberFormat('fr-FR', { style: 'currency', currency: 'EUR' }).format(amount);
+ }
+
+ document.getElementById('calculate').addEventListener('click', () => {
+ const amount = parseFloat(document.getElementById('amount').value);
+ const duration = parseFloat(document.getElementById('duration').value);
+ const rate = parseFloat(document.getElementById('rate').value);
+
+ if (isNaN(amount) || isNaN(duration) || isNaN(rate)) {
+ alert('Veuillez remplir tous les champs correctement.');
+ return;
+ }
+
+ const { monthlyPayment, totalCost } = calculateLoan(amount, duration, rate);
+ document.getElementById('monthlyPayment').textContent = formatCurrency(monthlyPayment);
+ document.getElementById('totalCost').textContent = formatCurrency(totalCost);
+ });
+
+ document.getElementById('compare').addEventListener('click', () => {
+ const rates = Array.from(document.querySelectorAll('.rate')).map(input => parseFloat(input.value)).filter(rate => !isNaN(rate));
+ if (rates.length < 2) {
+ alert('Veuillez entrer au moins deux taux pour comparer.');
+ return;
+ }
+
+ const amount = parseFloat(document.getElementById('amount').value);
+ const duration = parseFloat(document.getElementById('duration').value);
+
+ if (isNaN(amount) || isNaN(duration)) {
+ alert('Veuillez d\'abord entrer un montant et une durée.');
+ return;
+ }
+
+ const comparisonResults = document.querySelector('.comparison-results');
+ comparisonResults.innerHTML = '';
+
+ rates.forEach(rate => {
+ const { monthlyPayment, totalCost } = calculateLoan(amount, duration, rate);
+ const resultDiv = document.createElement('div');
+ resultDiv.className = 'result-card';
+ resultDiv.innerHTML = `
+
Mensualité: ${formatCurrency(monthlyPayment)}
+
Coût total: ${formatCurrency(totalCost)}
+ `;
+ comparisonResults.appendChild(resultDiv);
+ });
+ });