Question Bank

Array Higher-Order Methods Quiz (map / filter / reduce / find)

Difficulty: Easy

Four drills on the core array higher-order methods: reduce for aggregation, filter plus reduce (or Math.max) for selection, find for first-match lookup, and flatMap for one-step map-and-flatten.

Question Bank
/

Array Higher-Order Methods Quiz (map / filter / reduce / find)

Array Higher-Order Methods Quiz (map / filter / reduce / find)

Four drills on the core array higher-order methods: reduce for aggregation, filter plus reduce (or Math.max) for selection, find for first-match lookup, and flatMap for one-step map-and-flatten.

Question Bank
Easy
JavaScript
4 questions
quiz
map-filter-reduce
arrays
fundamentals

523 views

15

Use Array.prototype.reduce to compute the sum of an array of numbers. Your solution must work on an empty array (returning 0) and must not mutate the input.

Examples

Example 1:

Input: [1, 2, 3, 4, 5]
Output: 15
Explanation: `reduce((acc, n) => acc + n, 0)` folds the array into a single accumulator starting at 0.

Example 2:

Input: []
Output: 0
Explanation: The explicit `initialValue` of 0 makes the empty-array case safe; omitting it would throw `TypeError: Reduce of empty array with no initial value`.