JavaScript AcademyProfessional Engineering

Level 1 · Foundations Project 09 of 60 Runs in the browser

Project 9: Shopping Cart Price Calculator

A shopping cart that grows with you: from three variables in Stage 1 to a validated, persistent, tested browser application in Stage 10.

This is the academy's recurring example. Stages 1–2 fit Level 1; Stages 3–4 return in Level 2 (arrays and objects), Stages 5–9 in Level 3 (DOM, events, state, persistence) and Stage 10 in Level 5 (testing). Each stage links to the lessons it uses.

1 · The problem

Online shops must calculate totals correctly every time: quantities change, discount codes are typed wrongly, customers reload the page, and one rounding mistake costs money. This project builds that cart step by step, keeping every stage as its own working version.

User stories

  • As a shopper, I want to add products and see the total update immediately, so I know what I will pay.
  • As a shopper, I want to change a quantity or remove a product, so the cart matches what I need.
  • As a shopper, I want to apply a discount code and be told clearly if it is not valid.
  • As a shopper, I want my cart to still be there if I reload the page.
  • As a developer, I want the price logic separate from the page and covered by tests, so I can change it safely.

2 · Requirements

  • Products A (25), B (40) and C (15) are editable data, not hard-coded in the logic.
  • Users can add products, remove lines and change quantities.
  • The discount code SAVE10 gives 10% off the subtotal; other codes are rejected with a message.
  • Tax of 10% is charged on the discounted amount.
  • Every amount is rounded to cents and shown as $0.00.
  • Quantities must be whole numbers from 0 to 99; 0 removes the line.
  • The cart and discount code survive a page reload.
  • Product names and other data are rendered as text, never as HTML.
  • All logic is covered by automated tests, including boundary cases.

Acceptance criteria

  • Given 2 × Product A and 1 × Product B with SAVE10, the page shows subtotal $90.00, discount $9.00, tax $8.10 and total $89.10.
  • Typing abc as a quantity shows 'Enter a whole number.' and leaves the cart unchanged.
  • After a reload, the same cart lines and discount code are restored.
  • A product name containing HTML is displayed as text.

Architecture

LayerFileResponsibility
Dataproducts.jsProduct records (editable).
Logiccart.jsPure functions: addItem, removeItem, setQuantity, lineItems, totals. No DOM access, so it runs in Node tests.
Stateapp.jsOne state object, changed only by update(action) through reducer().
Presentationapp.jsrender(state) rebuilds the cart table and summary with textContent.
Persistenceapp.jssaveState / loadState with JSON in localStorage (validated on load).

Flow: User event → update(action) → reducer(state, action) → new state → saveState → render(state)

Expected result

Shopping Cart Price Calculator: finished app
finished app

Screenshots are captured automatically from the solution by the browser test suite.

Build it: guided stages

Pick a stage, load its starter files, follow the steps, then run the app and the tests. Every stage is preserved separately, so loading one never overwrites another.

  1. Stage 1: Variables and calculations · Calculate the subtotal, discount, tax and total for a fixed cart using variables and arithmetic.
  2. Stage 2: Functions · Move each calculation into a named function so it can be reused and tested.
  3. Stage 3: Arrays and objects · Represent products and cart lines as objects in arrays, so the cart can hold any products.
  4. Stage 4: Array transformations · Rewrite the cart logic as pure functions with map, filter, find and reduce.
  5. Stage 5: DOM rendering · Show the products and the cart on a web page, built from data with createElement and textContent.
  6. Stage 6: Event handling · Let the user add, remove and change quantities with buttons and inputs.
  7. Stage 7: Application state · Keep everything the page shows in one state object and change it only through actions.
  8. Stage 8: Input validation · Reject invalid quantities and unknown discount codes with clear, accessible messages.
  9. Stage 9: Browser persistence · Save the cart so it is still there after a reload, and survive corrupted data.
  10. Stage 10: Automated testing · Protect the finished cart with unit tests for the logic and acceptance tests for the interface.

Troubleshooting

The total shows NaN.
A quantity or price is not a number. Log the values; quantities from inputs are strings until you convert them with Number().
Clicking Add does nothing.
Check that the listener is on #product-list and that the button has data-add. Log event.target to see what was clicked.
The cart does not survive a reload on the website.
Use the Reload Preview button: it keeps the sandbox's storage. Running again with Run App also keeps it; Clear Storage empties it.
Opening index.html from disk shows a blank cart.
Keep products.js, cart.js and app.js next to index.html: the page loads them with <script> tags in that order.

Extension challenges

  1. Add a second discount code, BULK20, that only applies when the subtotal is at least $100. Write the boundary tests first.
  2. Show a per-line discount when a product has 5 or more items, and keep the totals tests passing.
  3. Add an 'Undo last change' button by keeping a history of previous state objects.

Downloads

Browser projects open directly from index.html. Node.js unit tests: run node --test in the project folder.

AI development prompts

Context: I am building the academy's Shopping Cart (Stage 8, validation). Task: review my parseQuantity function for edge cases. Constraints: do not rewrite the function; list inputs I have not handled and the output each should produce. Expected behaviour: whole numbers 0–99 accepted, everything else rejected with a message. Testing: give each edge case as a test I can add to tests.json.
Context: My cart total is $0.01 off for some carts. Task: help me find where rounding happens and whether it is applied consistently. Constraints: ask for my totals() function before suggesting changes. Expected behaviour: all amounts rounded to cents once per step. Testing: suggest two carts that would expose the problem.
Context: I want to add BULK20 (20% off when the subtotal is at least $100). Task: help me plan the change with tests first. Constraints: keep cart.js free of DOM code; existing tests must still pass. Expected behaviour: boundary at exactly $100. Testing: list the tests to write before the implementation.

Source

Repository folder: projects/project-09-shopping-cart-price-calculator/ · view on GitHub (repository pending publication)