JavaScript Array Pair Extraction: Two Approaches Quiz
Split an array into consecutive pairs (with a trailing single when the length is odd), two ways (reduce with odd-index push and a stepped slice loop), plus companions on a generic chunk size and a lazy generator variant.
Question Bank
Hard
JavaScript
quiz
arrays
array-manipulation-patterns
interview-prep
1,040 views
12
Implement extractPairs(arr) using Array.prototype.reduce. At each even index, slice the next two elements and push them as a pair; skip the odd indices entirely. When the length is odd, the trailing slice naturally produces a one-element array.
Examples
Example 1:
Input: [0, 1, 2, 3]
Output: [[0, 1], [2, 3]]
Explanation: even indices 0 and 2 both slice a full pair.Example 2:
Input: [0, 1, 2, 3, 4]
Output: [[0, 1], [2, 3], [4]]
Explanation: index 4 slices a single-element trailing chunk.3 more questions, with full solutions and explanations, are available for premium members.
Upgrade to Premium