From 6a4b0eea9eea99924218894728a9a69151d94441 Mon Sep 17 00:00:00 2001 From: Josh Date: Tue, 4 Mar 2025 13:36:37 +0000 Subject: [PATCH] Fixed Expected Salary input fields and Calculation --- src/components/LoanRepayment.css | 72 +++++++++++++++++++-- src/components/LoanRepayment.js | 108 +++++++++++++++++-------------- 2 files changed, 126 insertions(+), 54 deletions(-) diff --git a/src/components/LoanRepayment.css b/src/components/LoanRepayment.css index bd2543f..20ecf92 100644 --- a/src/components/LoanRepayment.css +++ b/src/components/LoanRepayment.css @@ -1,9 +1,65 @@ -.loan-repayment-container { - padding: 20px; - background-color: #fafafa; - border-radius: 8px; - } +.loan-repayment-container { + width: 100%; + max-width: 500px; /* Adjust as needed */ + margin: auto; + padding: 20px; +} +.loan-repayment-container form { + display: flex; + flex-direction: column; + gap: 10px; + align-items: start; +} + +.loan-repayment-container .input-group { + display: flex; + align-items: center; /* Ensures labels and inputs are aligned */ + justify-content: space-between; + width: 100%; +} + +.loan-repayment-container label { + font-weight: bold; + width: 40%; /* Consistent width for labels */ + text-align: left; +} + +.loan-repayment-container input, +.loan-repayment-container select { + width: 58%; /* Consistent width for inputs */ + padding: 8px; + font-size: 16px; + border: 1px solid #ccc; + border-radius: 4px; + box-sizing: border-box; + margin-bottom: 10px; +} + +.loan-repayment-container .calculate-button-container { + display: flex; + justify-content: center; + width: 100%; + margin-top: 10px; +} + +.loan-repayment-container button { + width: 100%; + max-width: 300px; + padding: 10px; + background-color: green; + color: white; + border: none; + cursor: pointer; + border-radius: 4px; + font-size: 16px; + text-align: center; +} + + .loan-repayment-container button:hover { + background-color: green; + } + .loan-repayment-fields label { display: block; margin-bottom: 10px; @@ -131,4 +187,8 @@ font-weight: bold; margin-top: 15px; } - \ No newline at end of file + + .error { + color: red; + margin-top: 10px; + } diff --git a/src/components/LoanRepayment.js b/src/components/LoanRepayment.js index c32d149..a763b45 100644 --- a/src/components/LoanRepayment.js +++ b/src/components/LoanRepayment.js @@ -46,32 +46,42 @@ function LoanRepayment({ setLoading(true); const schoolResults = schools.map((school) => { const tuition = tuitionType === 'inState' ? school.inState : school.outOfState; - const monthlyRate = interestRate / 12 / 100; - const loanTermMonths = loanTerm * 12; + const monthlyRate = Number(interestRate) / 12 / 100; + const loanTermMonths = Number(loanTerm) * 12; const minimumMonthlyPayment = tuition * (monthlyRate * Math.pow(1 + monthlyRate, loanTermMonths)) / (Math.pow(1 + monthlyRate, loanTermMonths) - 1); - const extraMonthlyPayment = minimumMonthlyPayment + extraPayment; - let remainingBalance = tuition; - let monthsWithExtra = 0; - - while (remainingBalance > 0) { - monthsWithExtra++; - const interest = remainingBalance * monthlyRate; - const principal = extraMonthlyPayment - interest; - remainingBalance -= principal; - } + const extraMonthlyPayment = Number(minimumMonthlyPayment) + Number(extraPayment); + let remainingBalance = tuition; + let monthsWithExtra = 0; + + while (remainingBalance > 0) { + monthsWithExtra++; + + // Calculate interest for this month + const interest = remainingBalance * monthlyRate; + + // Ensure principal payment never goes negative + const principal = Math.max(extraMonthlyPayment - interest, 0); + + // Reduce balance + remainingBalance -= principal; + + // Prevent infinite loop in case of incorrect values + if (monthsWithExtra > loanTermMonths * 2) break; + } + const totalLoanCost = extraMonthlyPayment * monthsWithExtra; - let salary = salaryData.find((point) => point.percentile === selectedSalary)?.value || 0; + let salary = Number(salaryData.find((point) => point.percentile === selectedSalary)?.value || 0); let netGain = 'N/A'; let monthlySalary = 'N/A'; if (salary > 0) { const totalSalary = salary * loanTerm; - const currentSalaryEarnings = currentSalary * loanTerm * Math.pow(1.03, loanTerm); + const currentSalaryEarnings = Number(currentSalary) * loanTerm * Math.pow(1.03, loanTerm); netGain = (totalSalary - totalLoanCost - currentSalaryEarnings).toFixed(2); monthlySalary = (salary / 12).toFixed(2); } @@ -94,41 +104,43 @@ function LoanRepayment({ return (
{ e.preventDefault(); calculateLoanDetails(); }}> -
- - -
-
- - setInterestRate(e.target.value)} /> -
-
- - setLoanTerm(e.target.value)} /> -
-
- - setExtraPayment(e.target.value)} /> -
-
- - setCurrentSalary(e.target.value)} /> -
-
- - -
- -
- {error &&
{error}
} +
+ +
+
+ + setInterestRate(e.target.value)} /> +
+
+ + setLoanTerm(e.target.value)} /> +
+
+ + setExtraPayment(e.target.value)} /> +
+
+ + setCurrentSalary(e.target.value)} /> +
+
+ + +
+
+ +
+ + {error &&
{error}
} +
); }