The Shopping Cart
JavaScript
20 pts
The Shopping Cart
Build a cart object with a property items (an empty array) and four methods:
add(name, price)pushes an object{ name, price }intothis.items,count()returns the number of items,total()returns the sum of all prices (usereduce),receipt()prints one line per item and then the total (callthis.total()).
Add tea (15), bread (10) and cake (35), then call receipt(). Expected output:
tea - 15 Nakfa bread - 10 Nakfa cake - 35 Nakfa Total: 60 Nakfa
Checklist
- Give
cartthe four methods. - Store each item as an object and count and total them correctly.
- Use
reduceintotal()and reuse it inreceipt(). - Print the receipt.
🔑 Solution
If you view the solution before passing, you earn half points for this exercise. You still need to type it and press Check to finish.
How it works, step by step
Point at a step to highlight its lines. Type the solution yourself and press Check answer to finish.
