Help with the Chunky Monkey challenge

I’m doing the chunky monkey challenge. My current code can only return the first two parts of the array. I think the issue lies within the newArray part but I’m not 100% sure. Here’s the full code.

function chunkArrayInGroups(arr, size) {
 var newArray = arr.push();
  for (var i = 0; i < arr.length; i++) {
    arr = arr.slice(arr, size);

  }
  return arr;
}

chunkArrayInGroups(["r", "g", "x", "a"], 2);

Some observations:

What are you trying to do with the following line, because you do not use newArray anywhere else in your code.

 var newArray = arr.push();

The following line in the for loop:

    arr = arr.slice(arr, size);

tells me you do not understand how to use the slice function. Also, you are mutating arr at each iteration of the for loop, which is not what you really want to do. Refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice and look at the section on parameters and you will see you do not want to be using arr as a value for the first parameter.

Thanks for the link and the advice. I didn’t realise that my code had so many errors.

Really it is just the one line in the for loop, but there were two things going wrong inside that were complicating the issue. One suggestion would be to put a console.log statement before or after a section to see what the value of a variable is to understand what is happening. For example, you could do something like:

  for (var i = 0; i < arr.length; i++) {
    arr = arr.slice(arr, size);
    console.log(arr);
  }

to see what arr looks like after that line executes in the for loop for each iteration.