Here is the solution I tried that didn’t work and returns [[“a”, “b”]]:
function chunkArrayInGroups(arr, size) {
var newArr = [];
for (var i = 0; i < arr.length; i+=size) {
var slice = arr.slice(i, i + size);
var push = newArr.push(slice);
return newArr;
}
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
But my solution is not far off from the intermediate solution:
function chunkArrayInGroups(arr, size) {
// Break it up.
var arr2 = [];
for (var i = 0; i < arr.length; i+=size) {
arr2.push(arr.slice(i , i+size));
}
return arr2;
}
I am confused how mine is different than the intermediate. To me, they look like they should return the same, but mine doesn’t return the correct answer. Any insight as to why my code doesn’t work like I think it should? Thank you!