Last active
November 15, 2017 21:50
-
-
Save axdiamond/fe17b02443cf6c6e32a4c16efaa142a2 to your computer and use it in GitHub Desktop.
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 () { | |
let Andrew = { name: 'Andrew', debts: []}, | |
Justin = { name: 'Justin', debts: []}, | |
Jon = { name: 'Jon', debts: []}, | |
Zach = { name: 'Zach', debts: []}; | |
let people = [ | |
Andrew, | |
Justin, | |
Jon, | |
Zach | |
]; | |
let evenSplit = () => 1 / people.length; | |
let bills = [ | |
{ | |
name: 'Internet', | |
amount: 95, | |
billPayer: Justin, | |
getShare: evenSplit | |
}, | |
{ | |
name: 'Gas', | |
amount: 45, | |
billPayer: Jon, | |
getShare: evenSplit | |
}, | |
{ | |
name: 'Rent', | |
amount: 1500, | |
billPayer: Andrew, | |
getShare: (person) => { | |
if (person.name === Jon.name || person.name === Zach.name) | |
return 1 / 6; | |
else | |
return 2 / 6; | |
} | |
}, | |
{ | |
name: 'Electric', | |
amount: 90, | |
billPayer: Jon, | |
getShare: evenSplit | |
} | |
]; | |
// asign each person their debts | |
people.forEach((payer) => { | |
bills.forEach((bill) => { | |
if (payer.name !== bill.billPayer.name) { | |
let amountOwed = bill.getShare(payer) * bill.amount; | |
// if the payer already has a debt to the person, bundle it | |
const index = payer.debts.findIndex(debt => debt.debtor.name === bill.billPayer.name); | |
if (index > -1) { | |
payer.debts[index].amount += amountOwed; | |
} else { | |
payer.debts.push({ debtor: bill.billPayer, amount: amountOwed}); | |
} | |
} | |
}); | |
}); | |
// consolidate debts between people | |
people.forEach((payer) => { | |
payer.debts.forEach((debt) => { | |
// if the person we own money to owes us money or vice versa, consolidate | |
const index = debt.debtor.debts.findIndex(debt => debt.debtor.name === payer.name); | |
if (index > -1) { | |
const amountOwed = debt.debtor.debts[index].amount; | |
// balence it | |
if (amountOwed === debt.amount) { | |
debt.debtor.debts[index].amount = 0; | |
debt.amount = 0; | |
} else if (amountOwed > debt.amount) { | |
debt.debtor.debts[index].amount -= debt.amount; | |
debt.amount = 0; | |
} else { | |
debt.amount -= debt.debtor.debts[index].amount; | |
debt.debtor.debts[index].amount = 0; | |
} | |
} | |
}); | |
}); | |
// clear out empty debts | |
people.forEach((payer) => { | |
payer.debts = payer.debts.filter(item => item.amount > 0); | |
}); | |
// report | |
people.forEach((payer) => { | |
payer.debts.forEach((debt) => { | |
console.log(payer.name + ' owes ' + debt.debtor.name + ' $' + debt.amount); | |
}); | |
}); | |
})(); |
you forgot to send me 1% of the sum of all debts owed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the simple version that doesn't do advanced consolidation.
It only consolidates depts between 2 parties, not the whole pot.....yet