Chunky Monkey code understanding

Hey everyone I need some clarification and help understanding.

I have completed this challange but I would really like more help understanding it. Please don’t flame me to much for breaking this down as much as I am about to.

This was my original code for “Chunky Monkey”

function chunkArrayInGroups(arr, size) {
  var newArray = [];
  for (var i = 0, a= 0; i < arr.length; i++) {
    newArray.push(arr.slice(a , size));    
    a += size;
    size += size;
  return newArray; 
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2);

This returns this result: [[0,1][2,3][]]

I cannot understand why it did not return something else. In my head the code is saying this: step by step;

  1. We have a function.
  2. Hey I have an var which is ‘newArray’ and it equals ‘[]’
  3. Hey I have another var which is ‘loops’ and it equals ‘arr.length / size’
  4. Alright we are gonna do this ‘for’ loop.
  5. newArray now = newArray.push(arr.slice(0, 2));
    a. newArray = newArray.push([0,1])
    b. newArray = [[0,1]]
  6. newArray now = newArray.push(arr.slice(2, 4));
    a. newArray = newArray.push([2,3])
    b. newArray = [[0,1],[2,3]]
  7. newArray = newArray.push(arr.slice(4,6));
    a. newArray = newArray.push([4,5]);
    b. newArray = [[0,1],[2,3],[4,5]]
  8. Alright im done with ‘for’ loop here s ‘newArray’ like you asked.

I don’t understand why it is not returning the last array as [4,5] and adding that to the end of the array it had before. Instead it returns []… I don’t get it at all.

Now this is the code that works, and I get it fully.

> function chunkArrayInGroups(arr, size) {
>   var newArray = [];
>   for (var i = 0; i < arr.length; i += size) {
>     newArray.push(arr.slice(i , i + size));     
>   }
>   return newArray; 
> }
> chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2);

Returns: [[0,1],[2,3],[4,5]]

So except for the fact that the working code looks cleaner then my original, I really don’t see why it would return a different outcome.

Thank you in advance for the help and clarification.

1 Like


You are just missing one tiny detail—the problem is this:

size += size

Which means that instead of slicing arrays of a fixed length (in this case 2), you are slicing increasingly larger intervals. If you log a and size, you will see something like this:

console.log(a, size) // First loop returns 0 2
console.log(a, size) // Second loop returns 2 4
console.log(a, size) // Third loop returns 6 8
// ... etc.

For this reason, you only get the results that you expect in the first two and then empty arrays for the rest of the loops.

I hope that helps. :slight_smile:

OMG Thank you so much. I 100% get it now. I don’t know why I didn’t catch that sooner. Finally I can get this problem off my mind and move onto the next one.

Out of curiosity, what is the “loops” variable for? It doesn’t look like you ever use it.

Haha I forgot I left that in. At first it was going to be a way for me to tell it how many loops for loops I wanted it to do, but then I noticed that it was pointless and I could do it a different way using “arr.length”. I meant to take it out before I posted this, but I guess I didn’t.

Thanks for pointing it out though, ill go edit it, so no one else thinks I’m to crazy.