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 3 of 7
A patch, not a rewrite
Larkspur wants a person to look at every change before it lands. The agent’s change arrives as a patch, in pieces.
This chapter: What is in a patch, and can you take only part of it?
About this chapter
You will learn
- A diff shows changed lines only
- A patch splits into hunks
- Each hunk can be kept or dropped
Words you’ll meet
- Diff
- The lines a change removes and adds, with the lines around them left as they were.
- Patch
- A change to a file, written as a diff, that can be applied to it.
- Hunk
- One run of changed lines in a patch; a patch may have several.
Step 1 of 4
The agent’s is a of three . Hunk 1 makes 50 count as bulk.
Untick hunk 1, then press Run tests.