Chunky Monkey Help

function chunkArrayInGroups(arr, size) {
  let newArray = [  ]; 

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

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

Need help understanding where I went wrong?

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

I can see two issues:

  1. i += 1 doesn’t look right, does it? After slicing the first size elements, don’t you need to skip the elements you have sliced already?
  1. I don’t think you are using Array.prototype.slice correctly. You need to include the index form which you want to start slicing and where you want to end. Something like arr.slice(i, i + size).
1 Like

Ahhhh thanks! It works now, but I don’t understand what i + size is communicating? how does i + size tell it where it needs to end?

The i is telling it to start slicing at the start of the array, but how is i + size the end? If you didn’t tell me I would have thought it would be arr.slice(i, size)

Well, let’s look at it practically. Let’s say you want to chunk [1,2,3,4,5,6,7,8] into groups of two elements so that you get something like [[1,2],[3,4],[5,6],[7,8]]. In that case, the value of arr is [1,2,3,4,5,6,7,8] and that of size is 2.

This is what happens in the for loop.

  • When i = 0, the expression i < arr.length will evaluate to true. We need to slice the first two elements in our array, arr, and push it into newArray. We slice the elements at indices 0 and 1. However, be aware that Array.prototype.slice(i, j) will slice elements from index i to index j - 1. Since we need to slice the first two elements we can use arr.slice(i, i + size which translates to arr.slice(0, 0 + 2) since i = 0 and size = 2.
  • Before incrementing i, you need to be aware, we have sliced the first two elements at indices 0 and 1 in the previous step. The next elements we need to slice are those at indices 2 and 3. So, by how much should we increment i this time? We need to increment i by 2 which is the value of size. Therefore i += size will translate to size = 0 + 2 .
  • When i = 2, the expression i < arr.length will again evaluate to true. We will slice the elements at indices 2, and 3 using arr.slice(2, 2 + 2) originating from arr.slice(i, i + size. And the process continues until you have chunked out the array into groups of size.

What if arr.length is not a multiple of size?

You will still get the required result. Invoking arr.slice(i, j) when j is greater than arr.length will slice from i to the last element.

I hope that helps.

1 Like

Thank you! Makes so much sense now.

newArray.push(arr.slice(i, i + size))

If I understand correctly, this line of code is basically saying the newArray should start the slice at 0 index and end at 2 index (i + size)…because i = 0 and size = 2. Now it has sliced the first arr properly and keeps slicing each element by i += size( aka 2)…giving you the answer of [[“a”, “b”], [“c”, “d”]] or whatever other answer you may need

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