'use strict'; // unit-price-comparison.cjs -- METHOD DECK (X063), price per unit for two package sizes and // which is cheaper. Standard consumer-math unit pricing: unit price = total price / quantity. function fmt(x, decimals) { return x.toLocaleString('en-US', { minimumFractionDigits: decimals, maximumFractionDigits: decimals }); } module.exports = { slug: 'unit-price-comparison', title: 'Unit price comparison', deck_category: 'currency', description: 'Computes the price per unit for two package sizes and reports which package is the better value.', inputs: [ { name: 'price_a', type: 'number', required: true, description: 'Total price of package A', min: 0 }, { name: 'quantity_a', type: 'number', required: true, description: 'Quantity (in a common unit, e.g. oz or count) of package A', min: 0 }, { name: 'price_b', type: 'number', required: true, description: 'Total price of package B', min: 0 }, { name: 'quantity_b', type: 'number', required: true, description: 'Quantity (in the same unit as quantity_a) of package B', min: 0 }, ], outputs: [ { name: 'unit_price_a', description: 'price_a / quantity_a' }, { name: 'unit_price_b', description: 'price_b / quantity_b' }, { name: 'cheaper', description: '"A", "B", or "tie"' }, ], steps: [ 'Compute the unit price of package A: unit_price_a = price_a / quantity_a.', 'Compute the unit price of package B: unit_price_b = price_b / quantity_b.', 'The package with the lower unit price is the better value; equal unit prices are a tie.', 'Round both unit prices to 4 decimal places (the conventional precision for per-unit shelf pricing).', ], constants: [], sources: [ { kind: 'url', value: 'https://portal.ct.gov/dcp/common-elements/publications/saving-money-with-unit-pricing', title: 'Saving Money with Unit Pricing', publisher: 'Connecticut Department of Consumer Protection', retrieved_utc: '2026-09-30T22:29:13Z', note: 'Confirmed live: unit price is the amount paid per unit of measure (per pound, ounce, quart, gallon, etc.), used to compare package sizes of different brands and sizes on an equal footing; the page works a worked example of two package prices reduced to a common per-pound unit price.' }, ], disclaimer: null, questionText(inputs) { return `Package A costs ${inputs.price_a} for ${inputs.quantity_a} units and package B costs ${inputs.price_b} for ${inputs.quantity_b} units. Which is cheaper per unit?`; }, run(inputs) { const { price_a, quantity_a, price_b, quantity_b } = inputs || {}; for (const [k, v] of [['price_a', price_a], ['quantity_a', quantity_a], ['price_b', price_b], ['quantity_b', quantity_b]]) { if (typeof v !== 'number' || !isFinite(v) || v < 0) throw new Error(k + ' must be a nonnegative finite number'); } if (quantity_a === 0) throw new Error('quantity_a must be greater than zero'); if (quantity_b === 0) throw new Error('quantity_b must be greater than zero'); const unitA = Math.round((price_a / quantity_a) * 10000) / 10000; const unitB = Math.round((price_b / quantity_b) * 10000) / 10000; let cheaper; if (unitA < unitB) cheaper = 'A'; else if (unitB < unitA) cheaper = 'B'; else cheaper = 'tie'; const verdict = cheaper === 'tie' ? 'the two packages are the same price per unit' : `package ${cheaper} is cheaper per unit`; const answer = `Package A: ${fmt(unitA, 4)} per unit; Package B: ${fmt(unitB, 4)} per unit; ${verdict}.`; return { answer, values: { unit_price_a: unitA, unit_price_b: unitB, cheaper } }; }, examples: [ { name: 'worked example', inputs: { price_a: 4.99, quantity_a: 16, price_b: 7.99, quantity_b: 32 }, expected: { answer: 'Package A: 0.3119 per unit; Package B: 0.2497 per unit; package B is cheaper per unit.' }, check: 'By hand: 4.99/16 = 0.311875 -> 0.3119; 7.99/32 = 0.2496875 -> 0.2497. 0.2497 < 0.3119, so B is cheaper.' }, { name: 'test vector: A is cheaper', inputs: { price_a: 3.00, quantity_a: 12, price_b: 5.00, quantity_b: 18 }, expected: { answer: 'Package A: 0.2500 per unit; Package B: 0.2778 per unit; package A is cheaper per unit.' }, check: 'By hand: 3.00/12 = 0.25 exactly; 5.00/18 = 0.277778 -> 0.2778. 0.25 < 0.2778, so A is cheaper.' }, { name: 'test vector: tie', inputs: { price_a: 10, quantity_a: 5, price_b: 20, quantity_b: 10 }, expected: { answer: 'Package A: 2.0000 per unit; Package B: 2.0000 per unit; the two packages are the same price per unit.' }, check: 'By hand: 10/5 = 2 exactly and 20/10 = 2 exactly, so both unit prices are equal: a tie.' }, ], };