15. Implement chunk(arr, size) — Split an Array into Groups

easy

A two-minute question that mostly tests whether you handle the edges without overthinking.

The idea

Walk the array in steps of size, slicing a window each time. slice quietly clamps past the array's end, so the final short chunk needs no special case.

Follow-ups

  • "size <= 0 or non-integer?" → return [] (or throw) — state your choice.
  • "Without slice?" → push into the current bucket and start a new one when it fills.
  • "Lazy/streaming chunks?" → a generator that yields each window.

What to practice

Write the slice version, then a generator function* chunk(iterable, size) for large inputs.

More questions

index.js
function chunk(arr, size) {
  // your code here
}

console.log(chunk([1, 2, 3, 4, 5], 2)); // [[1, 2], [3, 4], [5]]

Tests

Test Code

Enter JavaScript that runs after your solution. It should return a value or a Promise.