Verified change
Add discount codes
Add src/discount.js with applyDiscount(amount, code) supporting SAVE10 (10% off) and HALF (50% off). Unknown codes change nothing. Add tests.
Acceptance check
✓ Passed$ npm testExit 0Verification output
> demo-shop@1.0.0 test > node --test ✔ lineTotal multiplies price by quantity (0.415209ms) ✔ total sums line totals (0.118042ms) ✔ SAVE10 takes 10% off (0.442791ms) ✔ HALF takes 50% off (0.090708ms) ✔ unknown codes leave the amount unchanged (0.065291ms) ℹ tests 5 ℹ suites 0 ℹ pass 5 ℹ fail 0 ℹ cancelled 0 ℹ skipped 0 ℹ todo 0 ℹ duration_ms 65.090458
| @@ -0,0 +1,10 @@ | |||
| 1 | + | export function applyDiscount(amount, code) { | |
| 2 | + | switch (code) { | |
| 3 | + | case 'SAVE10': | |
| 4 | + | return amount * 0.9; | |
| 5 | + | case 'HALF': | |
| 6 | + | return amount * 0.5; | |
| 7 | + | default: | |
| 8 | + | return amount; | |
| 9 | + | } | |
| 10 | + | } |
| @@ -0,0 +1,15 @@ | |||
| 1 | + | import { test } from 'node:test'; | |
| 2 | + | import assert from 'node:assert/strict'; | |
| 3 | + | import { applyDiscount } from '../src/discount.js'; | |
| 4 | + | ||
| 5 | + | test('SAVE10 takes 10% off', () => { | |
| 6 | + | assert.equal(applyDiscount(100, 'SAVE10'), 90); | |
| 7 | + | }); | |
| 8 | + | ||
| 9 | + | test('HALF takes 50% off', () => { | |
| 10 | + | assert.equal(applyDiscount(100, 'HALF'), 50); | |
| 11 | + | }); | |
| 12 | + | ||
| 13 | + | test('unknown codes leave the amount unchanged', () => { | |
| 14 | + | assert.equal(applyDiscount(100, 'OTHER'), 100); | |
| 15 | + | }); |