Chunky Monkey (A Loop)

Hi! I have a problem with the loop as I understood, but I don’t know what is the bug. I mean, I understand that the condition is not correct but what exactly???
I thought that my code is logical. Please, help. I would be very grateful.
Sorry, if I asked a stupid question. Just learning!


function chunkArrayInGroups(arr, size) {
let one = [];
let two = [];
let everything = [];
for (let i = 0; i < size; i++) {
    one.push(arr[i]);
}
for (let j = size; j < arr.length; j++) {
    two.push(arr[j]);
}
everything.push(one, two);
return everything;
}

console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2)); //[ [ 'a', 'b' ], [ 'c', 'd' ] ]

console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3)); // [[0, 1, 2], [3, 4, 5]]

console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2)); //what I need: [[0, 1], [2, 3], [4, 5]], what I get: [ [ 0, 1 ], [ 2, 3, 4, 5 ] ]
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3)); // what I need: [[0, 1, 2], [3, 4, 5], [6]] what I get: [ [ 0, 1, 2 ], [ 3, 4, 5, 6 ] ]

Challenge: Chunky Monkey

Link to the challenge:

You are working under the assumptions that two arrays are enough to fit all the content.

This second loop, will consume “everything else” in the array up to that index.
But that’s not the correct logic.

You can clearly see it an example like this:

chunkArrayInGroups([1,2,3,4,5] ,1) // [[1], [2,3,4,5]]

So instead of one and two you should think more about as long as there are items in the array, take N and place them into a separate chunk.

Easier said than done of course.
Hope this helps :sparkles:

p.s. there’s a handy method with array called slice: maybe can help :wink:

2 Likes

Thank you so much!!!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.