Created
November 28, 2021 01:14
-
-
Save SethVandebrooke/b70fdb1f5b7d3fd3e8f33c63a1064cba to your computer and use it in GitHub Desktop.
create a plan for prioritizing and paying off debt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findBestPayoffSequence(loans) { | |
return loans.reduce(function (a, b) { | |
b.effeciencyScore = (b.monthlyPayment / b.amountLeft) * 100 | |
a.push(b) | |
return a | |
}, []).sort(function (a, b) { return a.effeciencyScore >= b.effeciencyScore }) | |
} | |
function createDebtKillingPlan(loans) { | |
let orderedLoans = findBestPayoffSequence(loans) | |
let monthlyPlan = [] | |
let m = 1 | |
while (orderedLoans.length > 0) { | |
let month = { month: m, loans: [] } | |
for (let i = orderedLoans.length - 1; i >= 0; i--) { | |
let loan = orderedLoans[i] | |
if (loan.amountLeft <= loan.monthlyPayment) { | |
if (i > 0){ | |
orderedLoans[i - 1].monthlyPayment += loan.monthlyPayment | |
orderedLoans[i - 1].amountLeft -= loan.monthlyPayment - loan.amountLeft | |
} | |
orderedLoans.splice(i, 1) | |
} else { | |
month.loans.push({ | |
name: loan.name, | |
amountLeft: loan.amountLeft - loan.monthlyPayment, | |
monthlyPayment: loan.monthlyPayment | |
}) | |
orderedLoans[i].amountLeft = loan.amountLeft - loan.monthlyPayment | |
} | |
} | |
monthlyPlan.push(month) | |
m++ | |
} | |
return monthlyPlan | |
} | |
createDebtKillingPlan([ | |
{ name: "Contingency Savings Payback", amountLeft: 3933, monthlyPayment: 100 }, | |
{ name: "New Car", amountLeft: 9000, monthlyPayment: 200 }, | |
{ name: "Loan Payback for Refinance", amountLeft: 3852, monthlyPayment: 171 }, | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment