# Chunky Monkey Challenge: Recursion Solution

Hi everyone, I just finished the JS Chunky Monkey Challenge, I solved it using a for loop and when I checked the challenge guide I saw that you could do it using recursion. I’ve been trying to wrap my head around it but I still don’t understand how, has anyone has an explanation for this? Thank you

That’s the code written on the guide (the goal is to 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) {
if (arr.length <= size) {
return [arr];
} else {
return [arr.slice(0, size)].concat(
chunkArrayInGroups(arr.slice(size), size)
);
}
}
``````

This is actually very similar to the freeCodeCamp lesson on writing a recursive “countdown” function. The fact that everything is being done in that return statement probably makes it much harder to see. Does it help if I do this:

``````function chunkArrayInGroups(arr, size) {
if (arr.length <= size) {
return [arr];
} else {
const afterChunk = arr.slice(size);
const oneChunk = arr.slice(0, size);
const chunkedArr = chunkArrayInGroups(afterChunk, size);
return [oneChunk].concat(
chunkedArr
);
}
}
``````
2 Likes

It does a little bit, thanks now I understand almost everything but I still have a problem understanding the `afterChunk`, we are using it as a argument with slice, means just a copy of the original array, as I see it, it’s always gonna be the first `size` elements? we are not changing where to start each time? If it makes sense.

`afterChunk` would be the part of the array from index `size` to the end of the array.

1 Like

Still have some issues with it so far but thank you so much for your help. I’ll keep searching about it

Feel free to ask more questions. It’s what we’re here for

1 Like

I don’t know if it’s too late to jump in, but I was (and maybe still am a little) struggling to understand what’s going on. So I broke it down. I’m going to pass an array directly into the function for the arr argument, and 2 for the size argument, and then I’m going to go through each return statement for chunkArrayInGroups function:

``````// 1st function call:
chunkArrayInGroups(  [3, 4, 2, 8, 9, 10, 200 ],  2 ) );
// its return statement:
// [ [3, 4] ].concat(chunkArrayInGroups( [2, 8, 9, 10, 200], 2 ) );

// 2nd function call:
chunkArrayInGroups( [2, 8, 9, 10, 200], 2 );
// its return statement:
// [ [2, 8] ].concat( chunkArrayInGroups( [9, 10, 200],  2 ) );

//  3rd function call:
chunkArrayInGroups( [9, 10, 200],  2 );
// its return statement:
// [ [9, 10] ].concat( chunkArrayInGroups( [200], 2 ) );

// 4th function call:
chunkArrayInGroups( [200], 2 );
// since it meets the if statment's condition ( [200].length <= 2 ),
// it returns [ [200] ]
// which triggers the  whole returning process:

[ [200] ] is returned which begins concatenation starting with to the 4th call of the chunkArrayInGroups function,
//which returns [ [200] ] in the 4th function call and concatenates that to [ [9, 10] ]
// which returns [ [9, 10], [200] ] in 3rd function call and concatenates that to [ [2, 8] ]
// which returns [ [2, 8], [9, 10], [200] ] in the 2nd function call and concatenates that to [ [3, 4] ]
// which returns [ [3, 4] , [2, 8], [9, 10], [200] ] in the 1st function call, finishing the recursive function.```

Writing that out helped me to see what's happening better. I hope it helps you. I hope you have a great day.``````
1 Like

Hey, thank you. This helps a lot and now and now I know what confused me, it’s because of the slice method, I find;t pay much attention to it so that’s what I thought was going on then:

``````// 1st function call:
chunkArrayInGroups(  [3, 4, 2, 8, 9, 10, 200 ],  2 ) );

// 2nd function call:
chunkArrayInGroups( [3, 4, 2, 8, 9, 10, 200 ], 2 );

//  3rd function call:
chunkArrayInGroups([3, 4, 2, 8, 9, 10, 200 ],  2 );
``````

As you can see it only makes scene the first time the function runs.

Thank you again for the explanation

You’re welcome. I’ve been doing my best to teach myself code by doing free tutorials and etc. I’m glad I could help even though I’m not a pro. Hopefully I will be able to get hired doing some remote work soon. Happy Coding!

1 Like

Best of luck man, hope you find a position soon

Thank you.

1 Like