Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Cart Total

Derived Stateeasy

Course · Section 11: Working With Arrays · Lecture 160: The reduce Method

Implement `cartTotal(items)` returning the sum of `price * qty` over all items.

Sample tests

Input: cartTotal([{"price":10,"qty":2},{"price":5,"qty":1}])
Output: 25
Input: cartTotal([])
Output: 0

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Storing the total in state instead of deriving it, which can drift out of sync.

Learning resources

  • React: Choosing the State Structure
Approach & explanation (try first)

The total is derived state: compute it from the cart with reduce on each render rather than storing a separate, duplicable value.

Loading...
⌘/Ctrl + Enter

Run your code to see results.