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
- A simple cart is counted and totalled
- A missing qty counts as 1
- Items with qty 0 or less are skipped (0 is not 1!)
- Items without a numeric price are skipped
- An empty cart gives 0 and 0
- The page lists the 3 counted items
- 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.