chunk([1, 2, 3, 4], 2) //--> [[ 1, 2], [3, 4]]
chunk([1, 2, 3, 4, 5], 2) //--> [[ 1, 2], [3, 4], [5]]
chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) //--> [[ 1, 2, 3], [4, 5, 6], [7, 8]]
chunk([1, 2, 3, 4, 5], 4) //--> [[ 1, 2, 3, 4], [5]]
chunk([1, 2, 3, 4, 5], 10) //--> [[ 1, 2, 3, 4, 5]]
My Solution
function chunk(array, size) {
const chunked = [];
for (let i = 0; i < array.length; i += size) {
chunked.push(array.slice(i, i + size));
}
return chunked;
}
SG Solution 1
function chunk(array, size) {
const chunked = [];
let index = 0;
while (index < array.length) {
chunked.push(array.slice(i, i + size));
index += size;
}
return chunked;
}