Basic Algorithm Scripting: Chunky Monkey **STUCK**

Tell us what’s happening:
I have been stuck on this exercise for a while and can’t seem to wrap my head around how it works. Here’s my code so far.

Your code so far


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

  return newArray;
}

chunkArrayInGroups(["a", "b", "c", "d"], 2);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 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

why do you have a loop if you never use the i variable?

1 Like

You’re only handling the case where you have the array split twice. It needs to be generalized to work for arrays of any length.

Let’s just say for example that the input is the following array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
and the second argument is 2 so you need to break it into arrays of length 2.

You’d need to loop over the original array so that on each iteration you can slice out what you need, something like this:

loop 1: [1, 2]
loop 2: [3, 4]
loop 3: [5, 6]
loop 4: [7, 8]
loop 5: [9, 10]

Like @ilenia said, you’ll need to use your i, and maybe play around with your incrementing, it doesn’t have to only be i++.

1 Like