Please tell how my code is wrong? Thank you!

Tell us what’s happening:
In console, everything logs perfectly fine but the test cases are failing. The only difference I can see is that my array have spaces is between.

  **Your code so far**

function chunkArrayInGroups(arr, size) {
let arr2 = [];
for(let i = 0; i < arr.length; i++){
  arr2.push(arr.slice(0, size));
  arr.splice(0, size);

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/98.0.4758.87 Safari/537.36

Challenge: Chunky Monkey

Link to the challenge:

What should this statement do? Why do you need it?

So, I added this to add the leftover element at the end of ‘arr2’.
For example in conditions like this - chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4) where there is a leftover element 8 needs to be added at the end.

*There is also a return arr2 at the end of the function which I somehow missed to add here above.

Sure, but the if statement has a condition which does not make sense. Do you agree?

1 Like

Using a for-loop in this situation is not useful.
Think again what you are doing and if there is a nother structure better suited for it.

This is the best advice actually.

After reading it again - it does make sense. The reason is that any value other than 0 is “truthy” → meaning it will get evaluated as true.
Hence the condition is checking if arr has any elements left which have to be pushed into arr2.
In other words if(arr.length) is identical to if(arr.length!=0)


Thanks, that does make sense. :slightly_smiling_face:

I appreciate you looking into my code.
May be this is not the correct way to achieve the solution but if you’ll log the function in console you’ll get the correct output, be it any argument.
I am still curious to know why the test cases are being failed when the logged output in the console is correct.
And if the reason is spaces in the logged arrays, then how those spaces are being added when there is no space there in the given argument.

Thanks again for the responses.

When I copy-paste your code, add the return arr2 → then it’s still failing the last test. Reason is because the for-loop is not appropriate for this challenge and passes the previous tests more as an accident.

Actually, I think this way may be possible. If you add a condition in the for loop to check if arr is bigger than size, and bring the last if condition outside the loop. This will work, seeing that arr is gradually becoming smaller.
Here Array.prototype.push() - JavaScript | MDN it says that push can be used with an array.
Or am I missing something?

EDIT: of course arr. length in the for loop should be devided by size.

It is, although adding just a condition without meaningful counter to the for-loop is bad practice as that’s not the purpose of a for-loop. Ofcourse calculating the number of loops beforehand by dividing the length by the size and then making a for-loop is a valid strategy.

Guess saying a for-loop would not be appropriate for this is not entirely correct. It can work with some additional preparations. I was just thinking about while-loops as the more suited approach, but it’s not the only option.

1 Like

Personally, I’d use a for loop that increments by the size and slice out the contents. I would not hack apart the arr inside of this for loop.

Its bad form to mutate the array as you iterate over it.

  1. This is a good way to write bugs. The logic for iterating over an array as you mutate it is messy and complicated.
  2. This is a good way to write code that is hard to maintain.
  3. Your function should not mutate inputs unless it is specifically requested.

Future you will appreciate it if you never develop the habit of mutating arrays as you iterate over them.


I was keeping in mind that at this place in the curriculum functional programming is not yet covered. With respect to not mutating anything unnecessarily.

1 Like

Sure, but you don’t need to know functional programming to know that if you have a loop with the exit condition i < arr.length but you keep changing arr.length, then your logic will be a massive, messy headache.


True, thanks for pointing that out.

1 Like

This won’t be a problem with this challenge because we will use slice, and this function does not mutate the original array.

The original code and the discussion was about the use of .splice():

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.

1 Like

Sorry but I thought you were talking about this particular challenge in general.

If we’re talking about his code, it’s missing a return statement, so everything is meaningless ;))

Hint: There is no need to use splice for this challenge. Play with indexes.

  arr2.push(arr.slice(0, size));