# Post Help: Chunky Monkey

Tell us what’s happening:
What are missing on my code

My code so far

``````
function chunkArrayInGroups(arr, size) {
let newArray = [];
for (let i = 0; i < arr.length; i++) {
newArray.push(arr.slice(0, size));
}
return newArray;
}

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

``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:83.0) Gecko/20100101 Firefox/83.0`.

Challenge: Chunky Monkey

Can you walk us a bit behind your train of thought for this?

As it is this function will:

• iterate over the length of the array, (in this case 4 times)
• save the first two element of the array (without changing it)

So your output for the example provided will be:

``````[ [ 'a', 'b' ], [ 'a', 'b' ], [ 'a', 'b' ], [ 'a', 'b' ] ]
``````

Few considerations:

Are you sure you actually need to loop over the whole length of the array? As I want to group them, maybe I should loop not just based on the array length, but also based on how many items I want to group together.

Are you sure about hard coding the starting index of `slice` to `0`. And what about the ending index? Maybe there’s a way to make it work with the loop index, since we are running inside a loop.

Hope this helps

Do you mean I should not start `slice` with `0` ?

Tell us what’s happening:
This code below is a solution number 2 of chunky monkey challenge Guide
link: freeCodeCamp Challenge Guide: Chunky Monkey

I did not understand the code too much specially this line: `newArray.push(arr.slice(i, i + size));`

``````function chunkArrayInGroups(arr, size) {
let newArray = [];
for (let i = 0; i < arr.length; i += size) {
newArray.push(arr.slice(i, i + size));
}
return newArray;
}

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

And that’s why I asked you to explain us your logic: because you were close, but were missing a few key points.

Let’s think in pseudo-code for a moment:

We have an array and a size param, and we want to group the item based on this size.

So we need to loop over the array, but we don’t really need to visit each element, just the ones that match with size. For example think about a [0 … 10] array, where the size is 2.

What we really care is looking at the number that will start the “boxing”:
0, 2 ,4, 6, 8.

How do we do that? We can simply increment the index of the loop by `size` as in:

``````for (let i = 0; i < arr.length; i += size)
``````

So that if the size this time is 4 we loop over: 0, 4, 8; and so on.

Now we need a way to “box” the values into a new array. You already decided to use `slice`, which is an appropriate choice… let’s head over the documentation to review it before implementing the code:

The `slice()` method returns a shallow copy of a portion of an array into a new array object selected from `start` to `end` ( `end` not included) where `start` and `end` represent the index of items in that array. The original array will not be modified.

`start` Optional. Zero-based index at which to start extraction.
and
`end` Optional
Zero-based index before which to end extraction. `slice` extracts up to but not including `end` . For example, `slice(1,4)` extracts the second element through the fourth element (elements indexed 1, 2, and 3).
[…]
If `end` is greater than the length of the sequence, `slice` extracts through to the end of the sequence ( `arr.length` ).

Interesting: seems like if we declare an end that is greater than the actual length, JS will guard us and give back up to length, that’s neat!

So now we already know how that our loop index is the “start” of where we want to box our values, so

• start = index

at this point end needs to start up to the actual size we need, so for example if size is 4:
[0 up to 4], [4 up to 8], [8 up to 12]
And we don’t even need to worry if `12 > array.length` as the documentation told us about. So

• end = index + size

or in code:

``````for (let i = 0; i < arr.length; i += size) {
newArray.push(arr.slice(i, i + size));
}
``````

Hope it’s clearer now.

Personal note: I know that sometimes it’s frustrating and all we want to do is progress. But I really feel like by looking at the suggestion you missed the opportunity of re-thinking your logic and come up with this solution yourself: as you see you were really close
Be more confident

Good luck and happy coding.

1 Like

No, I think you thought wrong!
I wrote my own code then I checked and looking on the solutions
My final code so far:

``````function chunkArrayInGroups(arr, size) {
let newArr = [];
for (let i = arr.length - 1; i > -1; i-= size) {
newArr.push(arr.splice(0, size));
}
return newArr;
}

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

``````

Thanks @Marmiz,
I want to ask you question
I solve Basic Algorithm Scripting by my own and I am happy of that.
for example, If I open this section `Basic Algorithm Scripting` after a month and click on any challenge (the hard ones of course ) but not remember the exact solution when read the challenge. Is it sense thing for me or for anyone? Or this my problem?

I have edited your last reply to include spoiler tags for those who haven’t worked on this problem yet.

As long as you understand what the problem is and how to arrive at a solution then it is totally fine if you don’t remember your exact solution from a month ago.

There are many ways to solve all of these algorithm challenges. If you are going to revisit a challenge try to solve it another way next time.

Also, doing more coding challenges like the intermediate algorithms, leetcode and codewars will strengthen your problem solving abilities and give you more tools to work with.

Thank @jwilkins.oboe

Today I started with edabit.com.
Do you recommend take `edabit.com` or

?

I haven’t used edabit before but after taking a quick glance at the site it has similar types of problems like codewars and leetcode.

How to add spoiler on any code?

If you look at your post you will see that I added `[spoiler]` at the top and `[/spoiler]` at the bottom to blur out the solution.

1 Like