I really confused about this question and answer, like the line of " if (a % size !== size - 1) temp.push(arr[a]);",and I checked the output with console.log, it turns out " [(2)[…],(2)[…]]", this is the right answer that he wants? Anybody can help me with this? Thanks!

Question goes like this:
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.

``````  **Your code so far**
``````
``````
function chunkArrayInGroups(arr, size) {
let temp = [];
let result = [];

for (let a = 0; a < arr.length; a++) {
if (a % size !== size - 1) temp.push(arr[a]);
else {
temp.push(arr[a]);
result.push(temp);
temp = [];
}
}

if (temp.length !== 0) result.push(temp);
return result;
}

chunkArrayInGroups(["a", "b", "c", "d"], 2);
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36`

Challenge: Chunky Monkey

This is a working solution to an FCC challenge so I have added `[spoiler][/spoiler]` tags.

So here you need to split the array first into chunks of size. For that, you need to divide arr.length by size. Make sure you do integer division to avoid problems. You will get a quotient and remainder. Take size number of elements quotient times from the original array . Those quotient number of arrays will be the first few arrays in the final array. And then if there is any remainder on division, then those number of remaining elements should be put into a new array and pushed in the final array.

1 Like

Then why did you write it?

Personally, I think that this:

``````  if (a % size !== size - 1) temp.push(arr[a]);
else {
temp.push(arr[a]);
result.push(temp);
temp = [];
}
``````

is sloppy. You are doing the same push in both cases. In pseudo code, this would be

``````if the remainder of a and size is NOT one less than the size
push a onto temp
else
push a onto temp
push temp onto result
reset temp
``````

To me, this makes more sense:

``````push a onto temp
if the remainder of a and size IS one less than the size
push temp onto result
reset temp
``````

That if logic is just checking if we have enough elements in the temp array, if we have the size of chunk that we want.

I got what you said, you’re right, but could you tell me why the console shows like the following?

It’s saying that you have an array with two items. Each of those two items is itself a an array of two items. The console has decided that the inner arrays are too long to show so it is showing the contents with an ellipsis. Different environments make different decisions about what to show in different situations. JS is just sending a reference to an object and the console is deciding what and how to show it.

If you want to ensure to show it all, you can convert it to a string:

``````const answer = chunkArrayInGroups(["a", "b", "c", "d"], 2);
``````console.log(JSON.stringify(answer, null, 2));