Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Chunk Generator

Beyond the Coursehard

Implement `chunkGen(arr, size)` using a generator that yields successive chunks of `size`, collected into an array of arrays.

Sample tests

Input: chunkGen([1,2,3,4,5], 2)
Output: [[1,2],[3,4],[5]]
Input: chunkGen([], 2)
Output: []

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • A wrong step size overlapping or skipping elements.

Learning resources

  • MDN: Iterators and generators
Visualize this concept: Iterators and Generators →
Approach & explanation (try first)

Yielding successive size-length slices lazily chunks the array, collected with spread.

Loading...
⌘/Ctrl + Enter

Run your code to see results.