Chunky Monkey... i'm stuck here

Tell us what’s happening:

i know that what’s inside the for loop is wrong cause i’m not getting the expected result but other than that i still can’t of a way of how to fix it

Your code so far


function chunkArrayInGroups(arr, size) {
let newArr = [];
for(let i = 0; i < arr.length; i++){
  newArr.push(arr.slice(i, size));
}
return newArr;
}

console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3));

the output that i’m getting :

[ [ 0, 1, 2 ], [ 1, 2 ], [ 2 ], , , ]

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36.

Challenge: Chunky Monkey

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/chunky-monkey

i modified the loop statement

for(let i = 0; i < arr.length; i += size){
    newArr.push(arr.slice(i, i + size ));
  }

this way i’m not looping over every element in the array and the slice method works just fine cause it’s slicing from the value right after the one it sliced before

1 Like

pointing out my errors was just as much help as i needed. thanks

1 Like