Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Follow 750 Pence Through the Shop

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’s price: whole pence, not pounds.
  • orderLines() in src/orders/catalogue.js builds the order’s line by copying the book: unitPrice takes its price and title takes its title.
  • Watch out for names. In createOrder(), a variable called price holds the object priceOrder() returns, not a number of pence. Write down each step’s format, not only its name.
  • In priceOrder(), the loop multiplies unitPrice by quantity and paidItemsSubtotal becomes 750. requirePence() checks the price and the line total are whole pence. discountAmount() takes 20 percent, a saving of 150 pence. priceOrder() returns it as discount, next to the total, 600.
  • createOrder() copies those numbers onto the order, and there orderMessage() turns the 600 into text with formatPrice() from src/lib/money.js: £6.00. Then runCommand() saves it all to data/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, then paidItemsSubtotal is 750, discount 150 and total 600. The grossSubtotal above 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 shows Small Maps · £8.00, but order-2’s row still shows £6.00. Line 33 of public/admin/app.js, the staff desk’s code, shows why: appendText(row, 'td', pounds(order.total));. The row reads the order’s own total, 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-2 stays.
  • The value leaves the shop as text in three places: formatPrice() in src/lib/money.js adds a pound sign (the confirmation, £6.00), formatPrice() in src/reports/csv.js doesn’t (the weekly report, 6.00), and pounds() in public/admin/app.js adds one too (the staff desk, £6.00).
  • Both formatPrice() functions use .toFixed(). When you search the whole repo for it with git grep -n toFixed, you get three matches, those two and pounds(), 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:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In Exercise: Track Down the Two Prices, you found two formatPrice() functions. The confirmation’s adds a pound sign to your order’s six pounds, and the report’s leaves it off.

00:12 Both start from one number, Small Maps’ 750 pence, which Nadia’s stored order turns into 600. Grace’s question about discount codes comes down to how 750 becomes 600, and where each form of it is kept.

00:27 Here, you’ll follow that value through the shop. At each place, write down where it is and its format. Start where the value is stored. A search of data/shop.json, the shop’s records, finds it as book-1’s price.

00:44 It’s whole pence, not pounds. When staff place an order, createOrder() first calls orderLines() to build the order’s line. Two fields copy the book: unitPrice takes its price, and title takes its title.

01:00 Watch out on the next line. A variable called price holds no pence. It’s the object that priceOrder() returns. So write down each step’s format, not only its name.

01:12 In priceOrder(), follow the value. The loop multiplies unitPrice by quantity, and paidItemsSubtotal becomes 750. requirePence() checks the price and line total are whole pence.

01:26 discountAmount() takes 20 percent of that, a saving of 150 pence. It’s returned as discount, next to the total, 600. Back in createOrder(), those numbers are copied onto the order.

01:40 orderMessage() turns the 600 into text with formatPrice() from money.js: six pounds. Then runCommand() saves it all to the shop’s records.

01:52 That’s still a prediction, so check it against a run. Your shop already holds one: order-2, the order you placed for your trace. A search of data/shop.json finds it on line 127.

02:06 Its line keeps the title Small Maps and unitPrice 750. Then paidItemsSubtotal is 750, discount is 150, and total is 600. It matches what you wrote down. The gross subtotal above also counts free books.

02:22 Leave it aside.

02:25 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, start it again, and reload the staff desk.

02:38 Pause the video and predict order-2’s total on the staff desk.

02:46 Six pounds. The Book menu shows Small Maps at eight pounds, but order-2’s row didn’t follow it. Line 33 of the staff desk’s code shows why. The row reads the order’s own total, still 600 in the file.

03:02 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.

03:18 If you followed along, stop the shop, put book-1’s price back to 750, start it again, and reload. Your order-2 stays.

03:29 You’ve met two formatPrice() functions. Only money.js’s adds a pound sign. The staff desk shows six pounds too. Both formatPrice() functions use toFixed(), so search the whole repo for it.

03:43 It finds three: those two, and pounds() in the staff desk’s code. Its name says pounds, but it takes pence and returns text with a pound sign.

03:52 The orders table calls it on each order’s total.

03:56 That’s three separate functions. None 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 sign.

04:09 One amount would show two ways.

04:13 So to follow a value, start where it’s stored, and at every place, write down where it is and its format. Then 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, the three formatters should, or they drift apart.

04:33 Add the entry to your map. Next comes Exercise: Follow a Price to the Screen, where you’ll follow a price to what staff see.

Become a Member to join the conversation.