Write a function chunk(arr, size) that splits an array into groups of length size and returns them as a two-dimensional array. If the array cannot be split evenly, the final chunk should contain the remaining elements. For example, chunk([1, 2, 3, 4, 5], 2) should return [[1, 2], [3, 4], [5]].
function chunk(arr, size) {
// your code here
}
Rules:
- Return an empty array if the input array is empty.
- Assume size is always an integer greater than 0.
- Do not mutate the original input array.
Post your solution as a reply. Answer goes up in about a day.