In the last course, Nadia’s short refund had a clear right answer before you touched any code: Ada’s notes said what a refund should be, and your check printed 600p against the shop’s 450p. This time, Grace’s message is an idea for something new: “Students get 15% off.”
Before anyone builds that discount, or judges a change that claims to, someone has to say what “correct” would mean. In this lesson, you’ll write the intent of the change in four parts, find a real basket where two fair readings of Grace’s sentence give different totals, and turn that difference into a question for her. Start with what her sentence settles and what it leaves open.
Write the Request as Four Parts
A request says what someone wants in their own words and leaves most decisions to whoever builds it. Grace’s sentence doesn’t say how staff would apply the discount, what the 15 percent comes off, or how it fits with the shop’s existing codes.
The intent of a change is a written statement of what the change is for, made before anyone judges it. It has four parts:
- The problem: Why anyone wants the change
- What’s true afterward: What the shop does once the change is in
- What must not change: Behavior that people and code rely on today
- What’s out of scope: What this change won’t do, so nobody judges it for that
Grace’s sentence is the request, not the intent. Anything it doesn’t settle becomes a silent guess in the code unless you mark it and ask her. When you write the intent down, you see those guesses while they’re still cheap to fix.
This course assumes Intermediate JavaScript (modules, nested data, higher-order functions, classes, and custom errors), but not that you’ve written tests, since it teaches the testing it needs. In this lesson, you’ll write in your map.md and run one short check. In later lessons, you’ll judge changes against the intent you write here. First, put the shop on the commit this course uses:
If you took the last course, open a terminal in your second-draft-books folder and run only the last three commands. git switch main leaves your fix branch, which keeps your fixes, and git log --oneline -1 shows the commit.
If you’re arriving directly, install Git and a current version of Node.js. The examples use Node 26. Then run all the commands, and create an empty map.md in your home folder, outside the repository:
If you arrive directly, use the shop once before reading its code, as docs/FIRST-HOUR.md suggests. Run npm start, open http://127.0.0.1:3000/admin/, and place an order for one Small Maps for customer customer-1 with the code READ20. The order table shows £6.00, and a confirmation appears in outbox/. Note the order and its total in map.md as your first evidence, stop the server with Ctrl+C, and run npm run seed again. Then come back here.
The first two parts come from Grace. Write the problem and what’s true afterward in your own words, and mark each point her sentence doesn’t settle. For the third part, you look in the code for who uses it today and what they rely on. The fourth is a boundary you propose, so mark it as an assumption for Grace to confirm. Start on the third part by finding every caller of the pricing function:
$ git grep -n -F "priceOrder("
README.md:26:Now open `src/orders/pricing.js`, starting at `priceOrder()`. Predict how the
docs/FIRST-HOUR.md:11:Start at `priceOrder()` in `src/orders/pricing.js`. Its imports name the next
src/orders/create.js:16: let price = priceOrder(state, lines, chooseCode(input));
src/orders/pricing.js:4:export function priceOrder(state, lines, code) {
test/pricing.test.js:13: assert.equal(priceOrder(state, lines, 'READ20').total, 600);
test/pricing.test.js:22: const price = priceOrder(state, lines, 'READ20');
test/pricing.test.js:32: assert.equal(priceOrder(state, lines, 'OLD120').total, 0);
test/pricing.test.js:55: assert.throws(() => priceOrder(state, orderLines(state,
The shop’s only caller is on line 16 of src/orders/create.js, so every order placed at the staff desk goes through priceOrder(). The order then stores the result’s totals, and refunds read them. You don’t need the refund code now, but a student’s order must still refund no more than they paid, the rule on line 29 of Ada’s docs/ARCHITECTURE.md. The four lines in test/pricing.test.js are Edsger’s tests, which you’ll read in a later lesson.
Here are only the lines of priceOrder() that touch the discount, lines 15 to 19 of src/orders/pricing.js:
let discount = findDiscount(state, code);
let saving = discountAmount(paidItemsSubtotal, discount);
// Keep the legacy floor: old imported discount records could exceed the basket.
// See docs/issues/17.md before removing it.
if (saving > paidItemsSubtotal) saving = paidItemsSubtotal;
Line 28 of Ada’s docs/ARCHITECTURE.md holds two rules, and these lines keep both. Line 15 looks up a single code, which is where “only one discount code applies” lives on the order path, alongside the check in chooseCode() in src/discounts/policy.js. Line 19 is the legacy floor that keeps the other: “Order totals never go below zero.”
An intent that lists only what’s new would accept a change that adds the student discount but breaks READ20. Write all four parts in map.md, then compare yours with this one:
## Intent: student discount (Grace's request)
- Problem: students should pay less than other customers.
Ask Grace: what's it for, and how do staff apply it?
- Afterward: a student's order costs 15% less.
Ask Grace: 15% of what?
- Must not change: READ20 and WELCOME10 orders price
as today, one code per order (policy.js line 2,
pricing.js line 15), totals never below zero
(pricing.js line 19), refunds never above what
was paid (ARCHITECTURE.md line 29), prices for
everyone else
- Out of scope: checking who is a student.
Assumption, confirm with Grace: staff decide at
the desk, and the code doesn't check.
Keeping one code per order means a student who brings READ20 gets one discount, not READ20 and the student discount together. That’s a decision too, and it’s now written where Grace can see it and disagree.
Your “Ask Grace” marks are the questions you’ll take back to her. “15% of what?” looks like a detail, but the next section shows that it changes what a customer pays.
Find Where Two Readings Part
A percentage needs something to be a percentage of, and priceOrder() works out two totals for every basket. It adds each line’s value to grossSubtotal, the full value of every book, and only the books the customer pays for to paidItemsSubtotal. A complimentary book, given away free, counts in the first and not the second. These are the same two totals that split Nadia’s refund.
That gives Grace’s sentence two fair readings:
- Paid books only: 15 percent of
paidItemsSubtotal, the way line 16 works out READ20’s saving today - The whole basket: 15 percent of
grossSubtotal, free books included, because a student might hear “15% off” as a saving on everything in the bag
Two readings matter only if some real basket makes them disagree. The seed order, order-1, is a candidate: Small Maps at 750p, The Long Way Home at 2250p, and a complimentary A Bookshop Window valued at 1000p. In your editor, create student-discount.mjs in your checks folder, starting with this supplied setup, the same as in the last course’s checks:
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
const file = resolve('src/orders/pricing.js');
const pricing = await import(pathToFileURL(file));
const text = await readFile('data/seed.json', 'utf8');
const state = JSON.parse(text);
Copy the setup as it is. You don’t need to understand it yet. It loads pricing.js and the seed data from the folder you run it in, so the synchronous check below has something to price. The await lines load files asynchronously, which is outside this lesson’s scope.
The rest of the check prices the three lines of order-1 with no code, then works out each reading’s total from the two subtotals. It rounds with Math.round() as discountAmount() does and takes each saving off paidItemsSubtotal, the amount line 25 charges:
const lines = state.orders[0].lines;
const price = pricing.priceOrder(state, lines, '');
console.log(price);
const paid = price.paidItemsSubtotal;
const ofPaid = Math.round(paid * 15 / 100);
const ofAll = Math.round(price.grossSubtotal * 15 / 100);
console.log('Paid books only: ' + (paid - ofPaid));
console.log('Whole basket: ' + (paid - ofAll));
Before you run it, work out both totals on paper: Do the two readings charge a student the same? Then run the check from your second-draft-books folder with node ~/checks/student-discount.mjs, or node $HOME\checks\student-discount.mjs in Windows PowerShell:
{
grossSubtotal: 4000,
paidItemsSubtotal: 3000,
discount: 0,
code: '',
total: 3000
}
Paid books only: 2550
Whole basket: 2400
The readings part by 150p. The paid-books reading saves 15 percent of 3000p, or 450p, so the student pays 2550p. The whole-basket reading counts the free book and saves 15 percent of 4000p, or 600p, so the student pays 2400p.
Now remove the difference on purpose. Change the line that assigns state.orders[0].lines to lines to const lines = state.orders[0].lines.slice(0, 2);, which drops the free book, and run the check again. Both readings print 2550. Without a complimentary book, the two subtotals are equal, so a check on an ordinary basket can’t tell you which reading a change follows. Put the line back afterward.
Neither total is wrong. Line 16 suggests the paid-books reading, since every code works that way today, but only Grace can say what she wants for students. Add the difference to your “Ask Grace” marks as a question she can answer without reading code:
When a student’s basket includes a free book, should the 15 percent come off only the books they pay for or the full value, free book included? For the
order-1basket, those readings give totals of £25.50 and £24.00, respectively.
A good question names a real basket and the result of each answer, so Grace decides the rule instead of leaving it to the code. To look for other places where her sentence could split, press the button below. It opens Code Mentor with the question ready to send:
Check each reading in the reply against the code before you add it to your map. A run distinguishes between readings of the calculation when it finds a basket where the totals differ. If you can’t find one, then use the code to explain why the totals always match before you drop the question. The two subtotals, for example, match whenever no book is free.
Other readings, such as who qualifies or what staff do when a student also brings READ20, can split while every total stays the same. For those, find a concrete situation where the two versions behave differently, and take each question you can’t settle to Grace.
Keep the Intent Apart From the Change
You can judge a change against an intent only if you wrote the intent before you saw the change. Suppose a made-up pull request for the student discount arrives tomorrow, described as “Adds a 15% student discount, taken off the basket’s full value so that complimentary books count too.” Its code does exactly that, so the order-1 basket comes to 2400p.
If you read that pull request first, you might summarize the intent as “15% off the full value,” which the change meets by definition. If the only way you can describe a change’s intent is by describing its code, you don’t have an intent yet.
Now compare the pull request with the intent in your map. The intent’s “Afterward” line asks “15% of what?” so the change has made a choice nobody has agreed to. Until Grace answers, you can’t call it right or wrong. Your review question is whether Grace chose the whole-basket reading. The rest of this course works the same way.
You write the intent from the request before you open any diff showing the proposed code changes, then compare.
When there’s no written request, ask whoever wanted the change for one, and write the four parts from their answer. To practice on a request you haven’t seen, the button below opens Code Mentor ready to ask for one:
Write your four parts before you send them, and expect questions about what must not change.
Check Your Understanding
Answer these with your map.md open:
- A teammate’s intent for the student discount reads: “Add a STUDENT15 code worth 15%.” Which of the four parts are missing, and what’s one thing it should list under “must not change”?
- Which of these baskets would make the two readings of “15% off” give different totals: two paid copies of A Bookshop Window, or one Small Maps with a complimentary A Bookshop Window?
- Another teammate says a student with READ20 should get 35 percent off. Which line of your intent does that break, and what would you ask Grace instead?
- A colleague looks at the made-up pull request and says, “It’s correct. It takes 15% off the full value, exactly as its description says.” What’s wrong with that reasoning?
- It’s an “afterward” line only, and it already guesses that the discount is a code. It has no problem, no “must not change,” and no “out of scope.” Under “must not change,” it could list READ20 and WELCOME10 orders pricing as today, one code per order, or totals never below zero.
- Small Maps with the complimentary A Bookshop Window. Its
grossSubtotalis 1750p and itspaidItemsSubtotalis 750p, so 15 percent of each gives a different saving. Two paid copies have the same 2000p in both subtotals, so both readings charge the same. - It breaks “one code per order,” which Ada’s
docs/ARCHITECTURE.mdlists on line 28 and line 15 ofsrc/orders/pricing.jsrelies on. Ask Grace which discount a student with READ20 should get, since the shop applies only one. - The colleague took the intent from the pull request’s description, so the change could only match it. The question is what Grace wanted, and she hasn’t said yet whether a free book counts toward the 15 percent.
Your next activity is the course’s first coding exercise, which runs in your browser. You change a file in the editor on the page, then click Run Tests. A failing test reports the expected and actual result, or an error. Change your code to match, then run the tests again. Full screen gives the editor more room, and the Contents tab below the editor explains the rest.
In Exercise: Find Where Two Readings Part, you’ll take another of Grace’s requests that the code doesn’t settle and write each reading as a small function. Then you’ll find a basket where they charge different totals while the shop’s existing orders still price as today.