Tell us what’s happening:
It looks like the array being passed, arr, is being split, but is not generating an array element. This is the output for the first iteration:
(4) [“a”, “b”, “c”, “d”]
VM31742:16 temp array = a,b
VM31742:18 new array = ,a,b
VM31742:16 temp array = c,d
VM31742:18 new array = ,c,d,c,d
Notice the console.log for arr? It shows the brackets around the entire array and the quotes around each element. In the console.log for tempArray, there are no brackets or quotes. So, I’m not sure why the split is not generating an array element.
Your code so far
function chunkArrayInGroups(arr, size) {
// Break it up.
var newArray = [[]];
var tempArray = [];
console.log(arr);
for (var i=0;i<arr.length;i+=size) {
tempArray.push(arr.slice(i,i+size));
console.log("temp array = " + tempArray);
newArray.push(tempArray);
console.log("new array = " + newArray);
tempArray.length = 0;
}
console.log("-------------");
return newArray;
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36
.
Link to the challenge:
https://www.freecodecamp.org/challenges/chunky-monkey