src/discount.ts: the agent’s patch
// Larkspur's bulk discount: 10% off an order line of 50 or more of one item, to the cent.export const BULK_QTY = 50;export const BULK_RATE = 0.1;/** Whether a line is big enough for the bulk discount. */export function isBulk(qty: number): boolean {−
return qty > BULK_QTY;+
return qty >= BULK_QTY;}/** The bulk discount on one order line, in dollars. */export function bulkDiscount(qty: number, unitPrice: number): number { if (!isBulk(qty)) return 0;−
return qty * unitPrice * BULK_RATE;+
return Math.round(qty * unitPrice * BULK_RATE * 100) / 100;}/** What one order line costs after its discount. */export function lineTotal(qty: number, unitPrice: number): number {−
return qty * unitPrice - bulkDiscount(qty, unitPrice);+
return Math.round(qty * unitPrice) - bulkDiscount(qty, unitPrice);}“50 should count as bulk, so > becomes >=. The comment says “to the cent”, so I round. And I tidy line prices to whole dollars.” You kept 3 of 3 hunks.
Chapter 4 of 7
Red to green
You can now keep or drop each hunk. Before the next review, the team asks you to guess what the tests will say.
This chapter: Can you tell from the hunks whether the tests will pass?
About this chapter
You will learn
- Each test checks one promise
- Red names the promise that broke
- The agent reads red and fixes it
Words you’ll meet
- Red
- A test run with at least one failure.
- Green
- A test run where every test passes.
Step 1 of 4
The agent’s patch is in the Code tab, all three hunks kept. Each test checks one promise the code makes.
Predict the test run.