JavaScript AcademyProfessional Engineering

Level 3 · Interactive Web Applications Project 28 of 60 Runs in the browser

Project 28: Interactive Shopping Cart

A shopping cart rendered from state: add products, change quantities, remove lines, apply a discount code with rules, and see shipping and totals update. Saved in localStorage.

Level 3 covers the DOM, events, forms, state and storage, so you write both files: the logic file holds pure state functions and app.js renders the state and handles events. Storage in the website's preview is memory-only; the downloaded project uses your browser's real localStorage.

1 · The problem

A small stationery shop needs a cart page. Prices must add up exactly, quantities have limits, discount codes have conditions, shipping is free above a threshold, and the cart must survive a page reload.

User stories

  • As a shopper, I want to see my total change as I edit quantities.
  • As a shopper, I want to know why a code did not work.
  • As a shopper, I want my cart kept if I close the tab.

2 · Requirements

  • Store prices in cents and format them as $0.00 only for display.
  • The cart is an array of { id, qty }; lines join it with the product list.
  • Add a product (quantity at most 10), change a quantity (0 removes the line), remove a line.
  • Codes: SAVE10 is 10% off; FIVEOFF is $5.00 off a subtotal of at least $20.00. Codes ignore spaces and capitals.
  • Shipping is $3.99, free when the total after discount is $30.00 or more, and $0 for an empty cart.
  • Announce additions, show code messages accessibly and save the cart.

Acceptance criteria

  • The cart starts with 2 Notebooks and 1 Sticky notes: subtotal $10.99.
  • Adding the Desk lamp gives $35.98.
  • FIVEOFF on $24.99 gives $5.00 off and a total of $23.98.
  • An unknown code shows 'That code is not valid.'

Architecture

LayerFileResponsibility
Pageindex.html + style.cssProduct list, cart table, totals, code form.
Statecart.jsMoney, lines, quantities, codes, totals. Your work.
View and eventsapp.jsRendering, events, storage. Your work.

Flow: Click/change → cart = addToCart / setQty / removeFromCart(cart, …) → save → renderCart() + renderTotals()

Expected result

Interactive Shopping Cart: 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: Products and cart · Join the cart with the products and draw both, with exact money.
  2. Stage 2: Add, change and remove · Change the cart through buttons and number inputs.
  3. Stage 3: Codes, shipping and saving · Apply discount rules, add shipping and save the cart.

Troubleshooting

Totals show values like 35.980000000000004.
Keep prices in cents and only divide by 100 in formatMoney.
Typing a quantity does nothing.
Listen for change on #cart-body; the inputs are replaced on every render.
A code still applies after removing items.
renderTotals checks the code again against the new subtotal.

Extension challenges

  1. Add a BUY2GET1 code for notebooks.
  2. Show how much more to spend for free shipping.
  3. Keep the accepted code in storage too.

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 Interactive Shopping Cart. Task: explain why the cart stores prices in cents. Constraints: explain the idea and point to the line that matters; do not write the whole solution. Expected behaviour: I can finish the stage myself. Testing: suggest two inputs that would show whether my code is right.
Context: My Interactive Shopping Cart passes the visible tests. Task: check my discount rules for edge cases such as a discount larger than the subtotal. Constraints: do not rewrite my code; list issues in order of importance. Expected behaviour: I understand each issue and can fix it. Testing: give one test case per issue.
Context: I finished the Interactive Shopping Cart. Task: help me plan this extension: show how much more to spend for free shipping. Constraints: keep the logic file free of page code; existing tests must still pass. Expected behaviour: a short plan with the functions to add or change. Testing: describe the new tests I should write first.

Source

Repository folder: projects/project-28-interactive-shopping-cart/ · view on GitHub