Chunk an array
MediumArrays · JavaScript
The task
Write chunk(arr, size) that splits arr into groups of size items and returns them as an array of arrays.
The last group may be smaller. An empty array returns an empty array.
Examples
- chunk([1, 2, 3, 4, 5], 2) returns [[1,2],[3,4],[5]]
Your code runs in your browser, in a separate worker, and is never sent to us. Edit the function, then press Run tests. Your work is saved here as you type.
JavaScriptCtrl or Cmd + Enter to run