Chucky Monkey (Algorithm Challenges)

I have been working on this problem for hours. From my output it looks like I am 90% there. I can’t seem to get why I am creating a sub-sub-array.

Output = [[[“a”,“b”]]],[[“c”,“d”]]]

I’m trying to get it to equal [[“a”,“b”],[“c”,“d”]]. Not sure how that extra sub array for each set is coming from.

function chunkArrayInGroups(arr, size) {
  // Break it up.
  var newArr = [];
  var newSlice;
  var j = 0;
  
  
  for (var i = 0; i < arr.length; i+= size){
    newArr[j] = [];
    newSlice = arr.slice(i, i+ size);
    newArr[j].push(newSlice);
    j++;
  }
  
  
  return newArr;
}

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

Well, you start with newArr = [] up top. Then you create a new array at newArr[j], later calling push() on that array, which gets pushed a third array from arr.slice(). You’re so close, you just have to delete some code!

1 Like

Thanks! I didn’t realize the slice “function” automatically created a new Array.

Removing the “J” portions fixed everything.

1 Like