Hi,
I’m working on the “Chunky Monkey” challenge -
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/chunky-monkey
My “Chunky Monkey” code seems to work on the Chrome console (and when I use console.log on the FCC site), but it doesn’t pass any of the tests.
- In my loop, I’m taking the whole ‘slice’ of the original array at once (starts with the value of i and stops with i+size) and assigning it the variable of ‘newSubArray.’
- I’m ‘pushing’ that ‘newSubArray’, as an array, into the new array (called ‘newArr’).
- With console.log, it’s complete, I’m displaying each sub-array separately. It seems to work each time, but it’s not passing the FCC tests at all, so I must be missing something.
What am I doing wrong?
Thanks!
function chunkArrayInGroups(arr, size) {
let newArr = [];
let newSubArr = [];
for (let i = 0; i < arr.length; i+= size){
newSubArr = arr.slice(i,i+size);
newArr.push([newSubArr]);
};
//just for checkin' :
for (let j = 0; j<(arr.length/size); j++){
console.log(newArr[j]);
}
console.log(newArr);
return newArr;
}
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4)