Car Loan vs. Personal Loan Comparison

Common Loan Details

Car Loan Details

Personal Loan Details

Error: Calculator components could not be loaded.

"; } }); function formatCurrencyCPL(value, showNegativeSign = true) { const num = Number(value); if (isNaN(num)) return "0.00"; let prefix = ""; let absNum = Math.abs(num); if (num < 0) { if (showNegativeSign) prefix = "-$"; else prefix = "$"; // Show absolute if negative sign not desired } else { prefix = "$"; } return prefix + absNum.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }); } function calculateMonthlyPaymentCPLHelper(principal, termMonths, annualRate) { if (principal <= 0 || termMonths <= 0) return 0; let safeAnnualRate = annualRate; if (annualRate < 0) safeAnnualRate = 0; const monthlyRate = safeAnnualRate / 12 / 100; if (monthlyRate === 0) { return principal / termMonths; } const payment = principal * (monthlyRate * Math.pow(1 + monthlyRate, termMonths)) / (Math.pow(1 + monthlyRate, termMonths) - 1); return (isNaN(payment) || !isFinite(payment)) ? 0 : payment; } function calculateComparisonCPL() { console.log("CPL calculateComparisonCPL called"); const principal = parseFloat(principalAmountInCPL.value); const rateCL = parseFloat(interestRateCLIn.value); const termCL = parseInt(loanTermCLIn.value); const ratePL = parseFloat(interestRatePLIn.value); const termPL = parseInt(loanTermPLIn.value); if (isNaN(principal) || principal <= 0) { alert("Please enter a valid Loan Amount (Principal) greater than 0."); return; } if (isNaN(rateCL) || rateCL < 0 || isNaN(termCL) || termCL <= 0) { alert("Please enter valid Interest Rate and Loan Term for the Car Loan."); return; } if (isNaN(ratePL) || ratePL < 0 || isNaN(termPL) || termPL <= 0) { alert("Please enter valid Interest Rate and Loan Term for the Personal Loan."); return; } // Car Loan Calculations const monthlyCL = calculateMonthlyPaymentCPLHelper(principal, termCL, rateCL); const totalCostCL = monthlyCL * termCL; const totalInterestCL = totalCostCL - principal; // Personal Loan Calculations const monthlyPL = calculateMonthlyPaymentCPLHelper(principal, termPL, ratePL); const totalCostPL = monthlyPL * termPL; const totalInterestPL = totalCostPL - principal; // Differences (Car Loan - Personal Loan) const diffMonthly = monthlyCL - monthlyPL; const diffInterest = totalInterestCL - totalInterestPL; const diffCost = totalCostCL - totalCostPL; // Update UI monthlyPaymentCLOut.textContent = formatCurrencyCPL(monthlyCL); totalInterestCLOut.textContent = formatCurrencyCPL(totalInterestCL >= 0 ? totalInterestCL : 0); // ensure non-negative interest display totalCostCLOut.textContent = formatCurrencyCPL(totalCostCL); monthlyPaymentPLOut.textContent = formatCurrencyCPL(monthlyPL); totalInterestPLOut.textContent = formatCurrencyCPL(totalInterestPL >= 0 ? totalInterestPL : 0); // ensure non-negative interest display totalCostPLOut.textContent = formatCurrencyCPL(totalCostPL); diffMonthlyPaymentOut.textContent = formatCurrencyCPL(diffMonthly); diffTotalInterestOut.textContent = formatCurrencyCPL(diffInterest); diffTotalCostOut.textContent = formatCurrencyCPL(diffCost); resultsSectionCPL.style.display = "block"; calculatedValuesCPL = { principal, carLoan: { rate: rateCL, term: termCL, monthly: monthlyCL, interest: totalInterestCL, total: totalCostCL }, personalLoan: { rate: ratePL, term: termPL, monthly: monthlyPL, interest: totalInterestPL, total: totalCostPL }, differences: { monthly: diffMonthly, interest: diffInterest, total: diffCost } }; } function generatePdfCPL() { console.log("CPL generatePdfCPL called"); if (Object.keys(calculatedValuesCPL).length === 0 || !resultsSectionCPL || resultsSectionCPL.style.display === 'none') { alert('Please calculate the comparison 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.'); return; } const { jsPDF } = window.jspdf; const doc = new jsPDF(); if (typeof doc.autoTable !== 'function') { alert('PDF generation plugin (jsPDF-AutoTable) is not loaded.'); return; } const data = calculatedValuesCPL; try { doc.setFontSize(18); doc.setTextColor(74, 20, 140); // Dark Purple #4a148c doc.text("Car Loan vs. Personal Loan Comparison", 14, 22); doc.setFontSize(10); doc.setTextColor(100); doc.text(`Report Generated: ${new Date().toLocaleDateString()}`, 14, 28); let startY = 38; const tableTheme = 'grid'; const headFillColor = [111, 66, 193]; // Purple #6f42c1 const headTextColor = [255, 255, 255]; // Inputs Summary doc.setFontSize(12); doc.setTextColor(90, 50, 163); // #5a32a3 doc.text("Loan Input Details", 14, startY); startY += 7; doc.autoTable({ body: [ ['Loan Amount (Principal)', `$${formatCurrencyCPL(data.principal)}`], ['Car Loan: Interest Rate', `${(data.carLoan.rate || 0).toFixed(2)}%`], ['Car Loan: Term', `${data.carLoan.term} months`], ['Personal Loan: Interest Rate', `${(data.personalLoan.rate || 0).toFixed(2)}%`], ['Personal Loan: Term', `${data.personalLoan.term} months`], ], startY: startY, theme: 'plain', styles: { fontSize: 10, cellPadding: 2 }, columnStyles: { 0: { fontStyle: 'bold' } } }); startY = doc.lastAutoTable.finalY + 10; // Results Table doc.setFontSize(12); doc.setTextColor(90, 50, 163); doc.text("Comparison Results", 14, startY); startY += 7; const resultsBody = [ ['Monthly Payment', `$${formatCurrencyCPL(data.carLoan.monthly)}`, `$${formatCurrencyCPL(data.personalLoan.monthly)}`, `$${formatCurrencyCPL(data.differences.monthly)}`], ['Total Interest Paid', `$${formatCurrencyCPL(data.carLoan.interest >= 0 ? data.carLoan.interest : 0)}`, `$${formatCurrencyCPL(data.personalLoan.interest >= 0 ? data.personalLoan.interest : 0)}`, `$${formatCurrencyCPL(data.differences.interest)}`], ['Total Loan Cost (P+I)', `$${formatCurrencyCPL(data.carLoan.total)}`, `$${formatCurrencyCPL(data.personalLoan.total)}`, `$${formatCurrencyCPL(data.differences.total)}`] ]; doc.autoTable({ head: [['Feature', 'Car Loan', 'Personal Loan', 'Difference (Car - Personal)']], body: resultsBody, startY: startY, theme: tableTheme, headStyles: { fillColor: headFillColor, textColor: headTextColor }, styles: { fontSize: 9 }, columnStyles: { 0: { cellWidth: 50, fontStyle: 'bold' }, 1: { halign: 'right' }, 2: { halign: 'right' }, 3: { halign: 'right' } } }); doc.save("Car_vs_Personal_Loan_Comparison.pdf"); } catch (error) { console.error("CPL PDF Error:", error); alert("An error occurred while generating the PDF: " + error.message); } }
Scroll to Top