Write a function that divides an array into chunks of size n, where n is the length of each chunk.
Examples
chunkify([2, 3, 4, 5], 2) ➞ [[2, 3], [4, 5]]
chunkify([2, 3, 4, 5, 6], 2) ➞ [[2, 3], [4, 5], [6]]
chunkify([2, 3, 4, 5, 6, 7], 3) ➞ [[2, 3, 4], [5, 6, 7]]
chunkify([2, 3, 4, 5, 6, 7], 1) ➞ [[2], [3], [4], [5], [6], [7]]
chunkify([2, 3, 4, 5, 6, 7], 7) ➞ [[2, 3, 4, 5, 6, 7]]
Notes
- It’s O.K. if the last chunk is not completely filled (see example #2).
- Integers will always be single-digit.
Solution:
function chunkify(arr, size) { let sol = []; while (arr.length) { let temp = []; let len = size; while (len--) { if (!arr.length) break; temp.push(arr.shift()); } sol.push(temp); } return sol; }
function chunkify(arr, size) { let sol = []; while (arr.length) { sol.push(arr.splice(0,size)); } return sol; }