Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Custom Reduce

A Closer Look at Functionshard

Course · Section 10: A Closer Look at Functions · Lecture 137: First-Class and Higher-Order Functions

Implement `myReduce(nums, op, initial)` that folds `nums` with the named operation `op` (`sum` or `product`) starting from `initial`.

Sample tests

Input: myReduce([1,2,3,4], "sum", 0)
Output: 10
Input: myReduce([1,2,3], "product", 1)
Output: 6

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Ignoring the provided initial value.
  • Assuming an operation that is not in the map.

Learning resources

  • MDN: Array.prototype.reduce
Approach & explanation (try first)

A custom fold selects a binary operation by name and reduces the array from the given initial value.

Loading...
⌘/Ctrl + Enter

Run your code to see results.