# Chunky Monkey: For loop problem

Chunky Monkey: For loop problem
0

#1

Tell us what’s happening:
So, in this code i am pushing in sub array’s into my array variable “array” and than filling it with as many elements from the passed in arr argument as size tells me to. Everything is working fine except the first for loop is only running twice instead of running the length of arr. What is the problem here?

Your code so far

`````` var array = [];
for (var i = 0; i < arr.length; i++){
array.push([]);
//pushes a sub-array into the array
for (var j = 0; j < size; j++){
array[i][j] = arr.shift();
//shifts the first element into the array, this process is repeated based on the 'size' arguement passed in
}

}
return array;
}

chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2);```

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

**Link to the challenge:**
https://www.freecodecamp.org/challenges/chunky-monkey``````

#2

I have left your code as you originally typed it, but I have added a series of console.log() statements, so you can hopefully see where your problem is. If you can still not figure it out after viewing and running the code below, let me know and I will tell you. I always feel it is better to let someone figure it out with new information rather than just be given the answer.

Once the code finishes loading, click the Run button (fat right arrow) to see the results on the right side.

#3

I understand the problem, by the time my first loop runs the second time, the arr.length has already been shrinked due to the .shift() method in my second loop. So, i just put the arr arguement within a variable and set my 1st for loop to less than that

#4

but, as you can see above, even though i used a variable duplicate and set i < duplicate.length, i run into the same problem.

#5

If you make any changes to the replit.com code I posted, only you can see them. Please repost your revised version, so we can see what you now have.