A two-minute question that mostly tests whether you handle the edges without overthinking.
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.
size <= 0 or non-integer?" → return [] (or throw) — state your choice.slice?" → push into the current bucket and start a new one when it fills.yields each window.Write the slice version, then a generator function* chunk(iterable, size) for large inputs.
function chunk(arr, size) { // your code here } console.log(chunk([1, 2, 3, 4, 5], 2)); // [[1, 2], [3, 4], [5]]
Test Code
Enter JavaScript that runs after your solution. It should return a value or a Promise.