Command Palette

Search for a command to run...

Hectal
Phase 2: Operators and Control Flow
JavaScript · Exercise 5 of 5 Previous
Intermediate

Order summary

Write summarize(cart). cart is an array of items like { name: "Tea", price: 20, qty: 2 }. It returns an object { count, total }: the number of units and the total price.

Rules: a missing qty (null or undefined) means 1. Skip items whose qty is 0 or less (they were removed). Skip items whose price is not a number.

Then show the order on the page: one <li> in #lines for each counted item (for example 2 × Tea = 40) and the total in #total (for example Total: 205).

What Check tests

  1. A simple cart is counted and totalled
  2. A missing qty counts as 1
  3. Items with qty 0 or less are skipped (0 is not 1!)
  4. Items without a numeric price are skipped
  5. An empty cart gives 0 and 0
  6. The page lists the 3 counted items
  7. The page shows the total 205

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.