Tell us what’s happening:
I got a basic solution. Then, I was staring at other solutions and I couldn’t completely get how the recursion solution works:
-
the first time recursion is used (to call
chunkArrayInGroups
),arr.slice(size)
is used (where size is 2, I guess) as the first new argument of; thensize
comes as the new second argument: it shouldn’t be 2, again? (but it should be 4, so something can be concat to the first slice, right?) -
when I analyzed the program in a debugger I saw that
arr
was [2, 3, 4, 5] at the first slice, as It would had been spliced and not sliced, so to speak… so, I missing something
Could you please help to solve this questions?
function chunkArrayInGroups(arr, size) {
if (arr.length <= size) {
return [arr];
} else {
return [arr.slice(0, size)].concat(
chunkArrayInGroups(arr.slice(size), size)
);
}
}
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2)); // [[0, 1], [2, 3], [4, 5]]
Your code so far
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 OPR/63.0.3368.107
.
Challenge: Chunky Monkey
Link to the challenge: