Arr.length not working correctly

Tell us what’s happening:
Describe your issue in detail here.
Sorry i’m a noob here, i just started coding few month ago from scratch. I can’t figure out why my code doesn’t show the real length of the array. I wold pass this challenge if only the length was the real one.


function chunkArrayInGroups(arr, size) {
let array = [];
for (let i = 0; i < arr.length; i++){
  let arr1 = arr.splice(0, size);
  array.push(arr1);
}
return array;
}

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

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

Challenge: Chunky Monkey

Link to the challenge:

where do you see the wrong length of the array?

also note that changing the array you are iterating over is not a good idea

function chunkArrayInGroups(arr, size) {
let array = [];
console.log(arr.length);

I tried console.log inside function and no matter how many objects you add inside chunkArrayInGroups to array the .length method works fine. It means it’s not an issue.

At the end i figured out how to solve it, thanks for the answers anyway :slight_smile:

Changing an array as you iterate over it is a recipe for bugs. I recommend never doing that.

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