PractitionerTest-driven practice · 2 of 3about 25 min

A cart total that survives the edge cases

Write cartTotal(items, coupon). items is an array of { price, quantity }; coupon is optional.

Rules: the total is the sum of price × quantity, rounded to 2 decimal places; an empty cart totals 0; the coupon "SAVE10" takes 10% off the sum, and coupon codes ignore case; any other coupon throws Error("Unknown coupon"); a quantity below 1 throws Error("Invalid quantity").

Before this: Guide: TDD in Practice, Challenge: Slugs: pass the tests you cannot see

Visible tests

test("adds price times quantity", () => {
  expect(cartTotal([{ price: 5, quantity: 2 }, { price: 3, quantity: 1 }])).to.equal(13);
});

test("applies SAVE10", () => {
  expect(cartTotal([{ price: 50, quantity: 2 }], "SAVE10")).to.equal(90);
});

Your code