Final project: a shopping cart
Build a small cart library from pure functions: add, remove, total and a text summary.
28 min
You will build the logic of a shopping cart, the sort of code that sits behind every shop page. A cart is an array of line objects:
[{ name: "pen", price: 1.5, qty: 4 }, { name: "book", price: 12, qty: 1 }]You will write four functions. Each one takes the cart and returns a new cart or a value, and never changes the cart it was given. That is the style used by most modern front-end code, because it makes bugs easy to find and changes easy to undo.
- 1.
addItem(cart, item): if an item with thatnameis already in the cart, add to itsqty; otherwise append the item. - 2.
removeItem(cart, name): return the cart without that item. - 3.
cartTotal(cart): total price, rounded to 2 decimals. - 4.
summary(cart): one text line per item"4 x pen = 6"(price times qty), joined with\n, then a final line"Total: 18". For an empty cart return"Cart is empty".
Write them one at a time, run the tests for each, and use the earlier functions inside the later ones.
Before you start
Write addItem, removeItem, cartTotal and summary as described in the lesson. Do not change the cart that is passed in.
Your code must satisfy
- addItem([], {name: "pen", price: 1.5, qty: 2})→[{"name":"pen","price":1.5,"qty":2}]add a new item
- addItem([{name: "pen", price: 1.5, qty: 2}], {name: "pen", price: 1.5, qty: 3})→[{"name":"pen","price":1.5,"qty":5}]add merges qty
- (() => { const c = [{name: "pen", price: 1.5, qty: 2}]; addItem(c, {name: "pen", price: 1.5, qty: 3}); return c[0].qty; })()→2add leaves original
- removeItem([{name: "pen", price: 1.5, qty: 2}, {name: "book", price: 12, qty: 1}], "pen")→[{"name":"book","price":12,"qty":1}]remove
- cartTotal([{name: "pen", price: 1.5, qty: 4}, {name: "book", price: 12, qty: 1}])→18total
- summary([{name: "pen", price: 1.5, qty: 4}, {name: "book", price: 12, qty: 1}])→"4 x pen = 6\n1 x book = 12\nTotal: 18"summary
function addItem(cart, item) {
}
function removeItem(cart, name) {
}
function cartTotal(cart) {
}
function summary(cart) {
}Make it yours
Add one more feature to the cart and prove it with tests. Ideas: a discount(cart, percent) function that returns the total after a percentage off; a mostExpensive(cart) that returns the name of the line with the highest total; a limit of 10 per item in addItem; or a sortBy(cart, field) function. Write it as a pure function, decide what it does for an empty cart, and test it with at least three inputs.
Deliverables
- 01Your new function and the four from the lesson
- 02Three test calls with the results you expect, including the empty cart
Paste your new function and your three test calls.
- Pure functions take data and return new data; they never change their inputs.
map,filter,reduceand spread build new arrays without mutation.- Build later functions on earlier ones instead of repeating logic.
- Decide the empty-case behaviour up front and test it.
Last checks.
Why does addItem return a new array instead of changing the old one?
- AIt is required by JavaScript
- BThe caller's data stays safe and bugs are easier to trace
- CIt is faster
- Dpush does not exist
Which builds the line total for one cart line l?
- A`l.price + l.qty`
- B`l.price * l.qty`
- C`l.price / l.qty`
- D`l.qty - l.price`
What does cart.filter(l => l.name !== "pen") do?
- ARemoves pen lines, returning a new array
- BRemoves pen from `cart` itself
- CReturns only pen lines
- DSorts the cart
What is cartTotal for 3 items at 2.5 each?
Which call would be a bug in a pure-function style?
- A`const next = addItem(cart, item)`
- B`cart.push(item)` inside addItem
- C`cart.map(...)`
- D`[...cart, item]`
Practise this lesson, not just read it
In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of JavaScript for Beginners is planned around your week. Free.