diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..2db8ba5 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -8,3 +8,15 @@ export function subtotal(items) { export function total(items) { return roundCents(subtotal(items) * (1 + TAX_RATE)); } + +// Splits the total (including tax) into whole-cent shares that sum exactly to it. +// Leftover cents go one each to the first people. +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..520ae57 --- /dev/null +++ b/tests/lib/splitbill.test.js @@ -0,0 +1,73 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { splitBill, total } from '../../src/lib/cart.js'; + +const items = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; // total 27 + +// Work in integer cents so float noise can't hide or fake a missing cent. +const cents = (amount) => Math.round(amount * 100); +const sumCents = (shares) => shares.reduce((sum, s) => sum + cents(s), 0); + +test('splitBill returns one share per person', () => { + assert.equal(splitBill(items, 3).length, 3); +}); + +test('splitBill splits an even total equally, including tax', () => { + assert.deepEqual(splitBill(items, 3), [9, 9, 9]); + assert.deepEqual(splitBill(items, 2), [13.5, 13.5]); +}); + +test('splitBill gives one person the whole total', () => { + assert.deepEqual(splitBill(items, 1), [total(items)]); +}); + +test('splitBill shares always add up to the cart total', () => { + 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, 7]) { + assert.equal( + sumCents(splitBill(cart, people)), + cents(total(cart)), + `cart total ${total(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: 4.25, qty: 2 }]; + for (const people of [2, 3, 4, 7]) { + const shares = splitBill(cart, people); + for (const share of shares) { + assert.ok(Math.abs(share * 100 - cents(share)) < 1e-9, `${share} is not a whole number of cents`); + } + const asCents = shares.map(cents); + assert.ok(Math.max(...asCents) - Math.min(...asCents) <= 1, `uneven split: ${shares}`); + } +}); + +test('splitBill hands out leftover cents instead of losing them', () => { + // 0.93 * 1.08 rounds to a total of 1.00, which does not divide by 3. + const shares = splitBill([{ price: 0.93, qty: 1 }], 3); + assert.deepEqual(shares.map(cents).sort((a, b) => a - b), [33, 33, 34]); +}); + +test('splitBill gives more people than cents a zero share', () => { + const shares = splitBill([{ price: 0.01, qty: 1 }], 3); // total 0.01 + assert.deepEqual(shares.map(cents).sort((a, b) => a - b), [0, 0, 1]); +}); + +test('splitBill of an empty cart is all zeros', () => { + assert.deepEqual(splitBill([], 3).map(cents), [0, 0, 0]); +}); + +test('splitBill rejects a number of people that is not a positive integer', () => { + assert.throws(() => splitBill(items, 0)); + assert.throws(() => splitBill(items, -2)); + assert.throws(() => splitBill(items, 1.5)); +});