What you're building
An ExpenseSplitter class for group trips or shared housing. Users log expenses, and the class figures out the minimum payments so everyone ends up even β a classic graph / balance problem.
Requirements
addMember(name)β register a participant. No-op if already added.addExpense(payer, amount, participants)βpayerpaidamounttotal, split equally amongparticipants(array of names, including the payer). The payer's share is deducted from what they're owed.getBalance(name)β return the net balance for a member: positive means others owe them, negative means they owe money, 0 means even.getSettlements()β return an array of{ from, to, amount }objects (amounts rounded to 2 dp) representing the smallest set of transactions that zeroes every balance. Order does not matter.
Stretch goals
- Enforce that all names in
participantsmust be registered members (throw an error otherwise). - Add
reset()to clear all expenses while keeping the member list.
What this proves
You can model a real financial algorithm, handle floating-point bookkeeping carefully, and produce a clean summary β the kind of feature in Splitwise, Tricount, and countless fintech apps.
β‘ 50 XP
solution.jsRuns in your browser
Loading editorβ¦
Tests3 to pass
getBalance reflects who paid
getSettlements zeroes balances
already-even group has no settlements
Run your code to check these.