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..4a8824d --- /dev/null +++ b/tests/lib/splitbill.test.js @@ -0,0 +1,81 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { splitBill, total } from '../../src/lib/cart.js'; + +// Compare money as integer cents so float noise in the test itself can't cause failures. +const toCents = (amount) => Math.round(amount * 100); +const sumCents = (shares) => shares.reduce((sum, share) => sum + toCents(share), 0); +const sortedCents = (shares) => shares.map(toCents).sort((a, b) => a - b); + +const items = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; // total 27.00 with 8% tax + +test('splitBill returns one share per person', () => { + assert.equal(splitBill(items, 3).length, 3); +}); + +test('splitBill gives one person the whole total, tax included', () => { + assert.deepEqual(splitBill(items, 1), [27]); +}); + +test('splitBill splits an evenly divisible total equally', () => { + assert.deepEqual(splitBill(items, 2), [13.5, 13.5]); + assert.deepEqual(splitBill(items, 3), [9, 9, 9]); +}); + +test('splitBill splits the taxed total, not the subtotal', () => { + // subtotal 10.00, total 10.80 + assert.deepEqual(splitBill([{ price: 10, qty: 1 }], 2), [5.4, 5.4]); +}); + +test('splitBill hands out leftover cents so shares add up exactly to the total', () => { + // 0.93 * 1.08 = 1.0044 -> total 1.00, which does not divide evenly by 3 + const shares = splitBill([{ price: 0.93, qty: 1 }], 3); + assert.deepEqual(sortedCents(shares), [33, 33, 34]); + assert.equal(sumCents(shares), 100); +}); + +test('splitBill shares always sum to total() and differ by at most one cent', () => { + const carts = [ + items, + [{ price: 0.93, qty: 1 }], + [{ price: 19.99, qty: 3 }, { price: 4.25, qty: 2 }], + [{ price: 0.01, qty: 1 }], + ]; + for (const cart of carts) { + for (const people of [1, 2, 3, 4, 6, 7]) { + const shares = splitBill(cart, people); + const cents = sortedCents(shares); + assert.equal(shares.length, people); + assert.equal(sumCents(shares), toCents(total(cart)), `sum for ${people} people`); + assert.ok(cents[cents.length - 1] - cents[0] <= 1, `shares within a cent for ${people} people`); + } + } +}); + +test('splitBill returns shares rounded to whole cents', () => { + for (const share of splitBill([{ price: 19.99, qty: 3 }], 7)) { + assert.ok(Math.abs(share * 100 - toCents(share)) < 1e-9, `${share} is not whole cents`); + } +}); + +test('splitBill gives zero shares when there are more people than cents', () => { + // total 0.02 split 3 ways: two people pay a cent, one pays nothing + const shares = splitBill([{ price: 0.02, qty: 1 }], 3); + assert.deepEqual(sortedCents(shares), [0, 1, 1]); +}); + +test('splitBill of an empty cart gives everyone zero', () => { + assert.deepEqual(splitBill([], 3), [0, 0, 0]); +}); + +test('splitBill rejects a non-positive or fractional number of people', () => { + assert.throws(() => splitBill(items, 0)); + assert.throws(() => splitBill(items, -2)); + assert.throws(() => splitBill(items, 2.5)); +}); + +test('splitBill does not modify the items', () => { + const cart = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; + splitBill(cart, 3); + assert.deepEqual(cart, [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]); +});