In this video, you followed one value, Small Maps’ 750 pence, through The Second Draft: from the book’s record, through pricing, into Nadia Patel’s stored order, and out to the three places that show it as text.
Key Points
- To follow a value, start where it’s stored, and at every place it goes, write down where it is and its format. Grace’s discount-code question comes down to how 750 becomes 600, and where each form of it is kept.
- In
data/shop.json, the shop’s records, 750 is book-1’sprice: whole pence, not pounds. orderLines()insrc/orders/catalogue.jsbuilds the order’s line by copying the book:unitPricetakes its price andtitletakes its title.- Watch out for names. In
createOrder(), a variable calledpriceholds the objectpriceOrder()returns, not a number of pence. Write down each step’s format, not only its name. - In
priceOrder(), the loop multipliesunitPriceby quantity andpaidItemsSubtotalbecomes 750.requirePence()checks the price and the line total are whole pence.discountAmount()takes 20 percent, asavingof 150 pence.priceOrder()returns it asdiscount, next to thetotal, 600. createOrder()copies those numbers onto the order, and thereorderMessage()turns the 600 into text withformatPrice()fromsrc/lib/money.js:£6.00. ThenrunCommand()saves it all todata/shop.json, the shop’s records.- A trace you’ve only read is a prediction. You can check it against
order-2, the Small Maps order you placed for your trace, which your shop already holds. Its line keeps"title": "Small Maps"and"unitPrice": 750, thenpaidItemsSubtotalis 750,discount150 andtotal600. ThegrossSubtotalabove them also counts free books, so you can leave it aside here. - Small Maps’ price now lives in two places: book-1’s record and order-2’s line. Stop the shop, change book-1’s price to 800 in
data/shop.json, start it again, and reload the staff desk (a restart doesn’t refresh a page that’s already open). The order form’s Book menu showsSmall Maps · £8.00, but order-2’s row still shows£6.00. Line 33 ofpublic/admin/app.js, the staff desk’s code, shows why:appendText(row, 'td', pounds(order.total));. The row reads the order’s owntotal, which was worked out once from the line’s 750 and is still 600 in the file. - That copy looks deliberate, since an order should remember what it sold for. That’s your reading of intent, not the code’s. So write down who reads which copy: the Book menu reads book-1’s price, and the orders table reads order-2’s total.
- If you followed along, stop the shop, put book-1’s price back to 750, start it again, and reload. Your
order-2stays. - The value leaves the shop as text in three places:
formatPrice()insrc/lib/money.jsadds a pound sign (the confirmation,£6.00),formatPrice()insrc/reports/csv.jsdoesn’t (the weekly report,6.00), andpounds()inpublic/admin/app.jsadds one too (the staff desk,£6.00). - Both
formatPrice()functions use.toFixed(). When you search the whole repo for it withgit grep -n toFixed, you get three matches, those two andpounds(), so no other code formats money with.toFixed(). - The name
pounds()says pounds, but the function takes pence and returns text with a pound sign. The orders table calls it on each order’s total. - None of the three calls another. So if Grace asked for prices without the pound sign, changing only money.js would change new confirmations while the staff desk kept its pound sign. One amount would show two ways.
- Mark each copy and each place that formats the same thing again, with whether it should change with the others: the order’s price shouldn’t, but the three formatters should agree. If only one of them changes, they drift apart.
Your Map Entry
Here’s the entry you added to your map in this video. Each numbered place says where the value is and its format, and the last lines mark the copy that should stay put. Compare it with yours, and keep whichever wording helps you find your way back later: