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.
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.
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`.Find the largest number in an array. Implement it with a higher-order method (either reduce or Math.max + spread) rather than a manual for loop, and handle the empty-array case by returning -Infinity (the identity for max).
Examples
Example 1:
Input: [3, 7, 2, 9, 4]
Output: 9
Explanation: `reduce` walks the array once, keeping the larger of `max` and the current element.Example 2:
Input: []
Output: -Infinity
Explanation: Seeding `reduce` with `-Infinity` gives a sensible identity for the empty case (any real number is greater than `-Infinity`).Given a list of users, return the FIRST user whose age is at least 18 using Array.prototype.find. If no such user exists, the function should return undefined. Explain in your solution why find is the right tool here instead of filter.
Examples
Example 1:
Input: [{name: 'Ana', age: 12}, {name: 'Bo', age: 19}, {name: 'Cy', age: 25}]
Output: { name: 'Bo', age: 19 }
Explanation: `find` short-circuits at the first element whose `age >= 18`, returning that user without scanning the rest of the array.Example 2:
Input: [{name: 'Z', age: 5}]
Output: undefined
Explanation: When no element satisfies the predicate, `find` returns `undefined` (not `null`), which composes well with optional chaining.Given a list of orders where each order has a lines array of item names, produce a single flat array of every item name across all orders. Use Array.prototype.flatMap rather than map(...).flat(), and explain why the one-step version is preferred.
Examples
Example 1:
Input: [{id: 1, lines: ['apple', 'bread']}, {id: 2, lines: ['carrot']}, {id: 3, lines: []}, {id: 4, lines: ['date', 'egg']}]
Output: ['apple', 'bread', 'carrot', 'date', 'egg']
Explanation: `flatMap` projects each order to its `lines` array and concatenates the results in a single pass. The empty `lines: []` order contributes nothing.Example 2:
Input: []
Output: []
Explanation: An empty input yields an empty output without throwing; `flatMap` handles the no-orders case naturally.