Chunky Monkey, could use some feedback on where I'm going wrong


Here’s what I’ve got:

function chunkArrayInGroups(arr, size) {
  var arrayChunks = [];

  for (i = 0; i < size; i++) {
  	arrayChunks.push(arr.slice(0, size));
  return arrayChunks;

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

which results in
[[“a”, “b”], [“a”, “b”]]

I understand what it’s doing wrong, but I can’t wrap my head around why; I feel like I’m missing something fundamental. Any hints, tips, or nudges in the right direction would be appreciated.


You are always slicing the same part of array:

arr.slice(0, size)

This is first thing. Slice does not remove elements. Do remove (and return) part of array you want to use splice method instead.

Second thing is your loop, it will work for this example, but you want to group X elements in Y groups of Z, not X elements in Y groups of Y.

Your loop should not run based on size, but instead arr.length / size.