Auto Loan EMI Calculator
Loan Summary
Monthly EMI:
$0.00
Total Interest Payable:
$0.00
Total Amount Payable (Principal + Interest):
$0.00
Error: Calculator components could not be loaded.
"; } else { console.log("EMI DOM elements loaded successfully."); } }); function formatCurrencyEMI(value) { const num = Number(value); if (isNaN(num)) return "0.00"; return num.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }); } function calculateEmiEMI() { console.log("EMI calculateEmiEMI called"); if (!loanAmountIn || !annualInterestRateIn || !loanTenureMonthsIn || !monthlyEmiOut || !totalInterestOut || !totalAmountOut || !resultsSectionEMI) { console.error("EMI: Missing one or more elements for calculation."); alert("Error: Calculator elements not found. Please refresh."); return; } const principal = parseFloat(loanAmountIn.value); const annualRate = parseFloat(annualInterestRateIn.value); const tenureMonths = parseInt(loanTenureMonthsIn.value); if (isNaN(principal) || principal <= 0) { alert("Please enter a valid Loan Amount greater than 0."); return; } if (isNaN(annualRate) || annualRate < 0) { alert("Please enter a valid Annual Interest Rate (0 or positive)."); return; } if (isNaN(tenureMonths) || tenureMonths <= 0) { alert("Please enter a valid Loan Tenure in months (greater than 0)."); return; } let emi = 0; let totalInterest = 0; let totalAmount = 0; if (annualRate === 0) { emi = principal / tenureMonths; totalInterest = 0; totalAmount = principal; } else { const monthlyRate = annualRate / 12 / 100; // EMI = P * i * (1+i)^n / ((1+i)^n - 1) const powerTerm = Math.pow(1 + monthlyRate, tenureMonths); emi = principal * monthlyRate * powerTerm / (powerTerm - 1); totalAmount = emi * tenureMonths; totalInterest = totalAmount - principal; } if (isNaN(emi) || !isFinite(emi)) { // Handle cases where calculation might lead to NaN/Infinity alert("Could not calculate EMI. Please check your inputs, especially for very small rates or tenures if principal is large."); emi = 0; totalInterest = 0; totalAmount = 0; // Reset values } monthlyEmiOut.textContent = `$${formatCurrencyEMI(emi)}`; totalInterestOut.textContent = `$${formatCurrencyEMI(totalInterest)}`; totalAmountOut.textContent = `$${formatCurrencyEMI(totalAmount)}`; calculatedValuesEMI = { principal: principal, annualRate: annualRate, tenureMonths: tenureMonths, emi: emi, totalInterest: totalInterest, totalAmount: totalAmount }; resultsSectionEMI.style.display = "block"; console.log("EMI Calculation successful:", calculatedValuesEMI); } function generatePdfEMI() { console.log("EMI generatePdfEMI called"); if (Object.keys(calculatedValuesEMI).length === 0 || !resultsSectionEMI || resultsSectionEMI.style.display === 'none') { alert('Please calculate the EMI first to generate a PDF summary.'); return; } if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF === 'undefined') { alert('PDF generation library (jsPDF) is not loaded.'); console.error("EMI PDF: jsPDF library not found."); return; } const { jsPDF } = window.jspdf; const doc = new jsPDF(); if (typeof doc.autoTable !== 'function') { alert('PDF generation plugin (jsPDF-AutoTable) is not loaded.'); console.error("EMI PDF: doc.autoTable is not a function."); return; } try { doc.setFontSize(18); doc.setTextColor(52, 58, 64); // #343a40 doc.text("Auto Loan EMI Calculation Summary", 14, 22); doc.setFontSize(10); doc.setTextColor(108, 117, 125); // #6c757d doc.text(`Report Generated: ${new Date().toLocaleDateString()}`, 14, 28); let startY = 38; const tableTheme = 'grid'; const headFillColor = [32, 201, 151]; // Teal #20c997 const headTextColor = [255, 255, 255]; // White // Input Data doc.setFontSize(12); doc.setTextColor(32, 201, 151); doc.text("Loan Inputs", 14, startY); startY += 7; doc.autoTable({ head: [['Parameter', 'Value']], body: [ ['Loan Amount', `$${formatCurrencyEMI(calculatedValuesEMI.principal)}`], ['Annual Interest Rate', `${(calculatedValuesEMI.annualRate || 0).toFixed(2)}%`], ['Loan Tenure', `${calculatedValuesEMI.tenureMonths} months`] ], startY: startY, theme: tableTheme, headStyles: { fillColor: headFillColor, textColor: headTextColor }, styles: { fontSize: 10 } }); startY = doc.lastAutoTable.finalY + 12; // Results Data doc.setFontSize(12); doc.setTextColor(32, 201, 151); doc.text("Calculation Results", 14, startY); startY += 7; doc.autoTable({ head: [['Description', 'Amount']], body: [ ['Monthly EMI', `$${formatCurrencyEMI(calculatedValuesEMI.emi)}`], ['Total Interest Payable', `$${formatCurrencyEMI(calculatedValuesEMI.totalInterest)}`], ['Total Amount Payable', `$${formatCurrencyEMI(calculatedValuesEMI.totalAmount)}`] ], startY: startY, theme: tableTheme, headStyles: { fillColor: headFillColor, textColor: headTextColor }, styles: { fontSize: 10 }, columnStyles: { 1: { fontStyle: 'bold' } } }); doc.save("Auto_Loan_EMI_Summary.pdf"); console.log("EMI PDF generated and download initiated."); } catch (error) { console.error("EMI PDF Error:", error); alert("An error occurred while generating the PDF: " + error.message + "\nPlease check the console for more details (F12)."); } }