What you're building
A splitBill(subtotal, tipPercent, people) function that returns an object describing how to fairly divide a restaurant bill.
splitBill(80, 20, 4)
// →
{
subtotal: 80,
tip: 16, // subtotal × tipPercent / 100, rounded to 2 dp
total: 96, // subtotal + tip
perPerson: 24, // total / people, rounded to 2 dp
}
Requirements
tipPercentis a plain percentage number (15 means 15 %, not 0.15)- All returned values rounded to 2 decimal places
- Throw an
Errorwith the message"people must be at least 1"whenpeople < 1 - Throw an
Errorwith the message"subtotal must be positive"whensubtotal <= 0
Stretch goals
- Add a
splitCustom(subtotal, tipAmount, people)that accepts a fixed tip dollar amount - Return each person's share as an array when people have different split ratios
What this proves
Input validation, arithmetic, and returning structured objects — the exact pattern used in payment microservices and point-of-sale software.
⚡ 40 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
basic split 4 people 20% tip
15% tip odd cents
throws on zero subtotal
throws on zero people
Run your code to check these.