diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..fb45a16 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -8,3 +8,13 @@ export function subtotal(items) { export function total(items) { return roundCents(subtotal(items) * (1 + TAX_RATE)); } + +export function splitBill(items, people) { + if (!Number.isInteger(people) || people < 1) { + throw new RangeError('people must be a positive integer'); + } + const totalCents = Math.round(total(items) * 100); + const base = Math.floor(totalCents / people); + const extra = totalCents - base * people; + return Array.from({ length: people }, (_, i) => (base + (i < extra ? 1 : 0)) / 100); +} diff --git a/tests/lib/splitbill.test.js b/tests/lib/splitbill.test.js new file mode 100644 index 0000000..6da9369 --- /dev/null +++ b/tests/lib/splitbill.test.js @@ -0,0 +1,89 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { splitBill, total } from '../../src/lib/cart.js'; + +// Work in integer cents so the assertions themselves aren't subject to float drift. +const cents = (amount) => Math.round(amount * 100); +const sumCents = (shares) => shares.reduce((sum, s) => sum + cents(s), 0); + +const items = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; // total 27 with 8% tax + +test('splitBill splits an evenly divisible total into equal shares', () => { + assert.deepEqual(splitBill(items, 2), [13.5, 13.5]); + assert.deepEqual(splitBill(items, 3), [9, 9, 9]); +}); + +test('splitBill includes tax in the shares', () => { + // subtotal 10, total 10.80 + assert.deepEqual(splitBill([{ price: 10, qty: 1 }], 2), [5.4, 5.4]); +}); + +test('splitBill with one person returns the whole total', () => { + assert.deepEqual(splitBill(items, 1), [total(items)]); +}); + +test('splitBill returns one share per person', () => { + for (const people of [1, 2, 3, 4, 7, 10]) { + assert.equal(splitBill(items, people).length, people); + } +}); + +test('splitBill shares always add up exactly to the total (no lost or extra cents)', () => { + const carts = [ + items, + [{ price: 10, qty: 1 }], + [{ price: 19.99, qty: 3 }, { price: 0.35, qty: 7 }], + [{ price: 0.01, qty: 1 }], + ]; + for (const cart of carts) { + for (const people of [1, 2, 3, 6, 7, 9, 13]) { + assert.equal( + sumCents(splitBill(cart, people)), + cents(total(cart)), + `cart ${JSON.stringify(cart)} split ${people} ways`, + ); + } + } +}); + +test('splitBill shares are whole cents and differ by at most one cent', () => { + const cart = [{ price: 19.99, qty: 3 }, { price: 0.35, qty: 7 }]; + for (const people of [2, 3, 6, 7, 9, 13]) { + const shares = splitBill(cart, people); + for (const share of shares) { + assert.ok(Math.abs(share * 100 - cents(share)) < 1e-6, `${share} is not a whole number of cents`); + } + const c = shares.map(cents); + assert.ok(Math.max(...c) - Math.min(...c) <= 1, `uneven split: ${shares}`); + } +}); + +test('splitBill gives leftover cents to the first people in the list', () => { + // total 10.80 = 1080 cents; 1080 / 7 = 154 remainder 2 + assert.deepEqual( + splitBill([{ price: 10, qty: 1 }], 7), + [1.55, 1.55, 1.54, 1.54, 1.54, 1.54, 1.54], + ); +}); + +test('splitBill handles a total smaller than one cent per person', () => { + // price 0.01 -> total 0.01 after tax rounding + assert.deepEqual(splitBill([{ price: 0.01, qty: 1 }], 3), [0.01, 0, 0]); +}); + +test('splitBill of an empty cart gives everyone zero', () => { + assert.deepEqual(splitBill([], 3), [0, 0, 0]); +}); + +test('splitBill rejects an invalid number of people', () => { + for (const people of [0, -1, 1.5, NaN, Infinity]) { + assert.throws(() => splitBill(items, people), `expected ${people} to throw`); + } +}); + +test('splitBill does not mutate the items', () => { + const cart = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; + const snapshot = structuredClone(cart); + splitBill(cart, 3); + assert.deepEqual(cart, snapshot); +});