Chunky monkey issue with for loop


Hi :slightly_smiling_face:, I’m doing chunky monkey.:monkey:
“Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.”

function chunkArrayInGroups(arr, size) {
var a = arr;
var b = [];
for ( var i = arr.length; i > 0; i - size) {


return b.pop();


I know there’s an infinite loop, I believe it has something to do with (i - size), but I can’t figure it out…
If anyone could help, it would be very appreciated, thanks.

Also, when I relocate “return b.pop()” above the curly braces, it sends out half of what i want, but won’t do the rest?


First of all, be sure to precede and follow your code with three backticks (below the ESC key) so it formats nicely in the forum:

As to your code, yes, that is the cause of your infinite loop. You are never changing i. The statement i - size doesn’t change i. I think you want:

for ( var i = arr.length; i > 0; i -= size) {

And I’m not sure about:

return b.pop();

Why are you popping? If you return b, you are a step closer.


Thank you, I’ll make sure I backtick in the future. Yea, the pop is from an old version where I was going to have “b” come equipped with a nested array, but I found out I can start an empty one.
Do you know why (i - size) won’t work? Thanks for the help.


Do you know why (i - size) won’t work?

Because it doesn’t do anything. That part of the for loop is supposed to tell what to do after each loop, usually changing your index variable. For example, usually you index i with something like i++. You are trying to subtract from i - that’s fine. But you aren’t changing i with (i - size) it evaluates a number but doesn’t change i. If you want it to reduce i by size each iteration, then you have to use i-=size.


It is done…
Thanks again.