My take on the chunky monkey problem

My take on the chunky monkey problem
0

#1
function chunkArrayInGroups(arr, size) {
  var newArr = []; //holds the new array with the chunks
  for (var i = 0; i < arr.length; i++) { //loop through the arr argument
    if (i % size === 0) { //checks to see if were at the size limit to slice the arr argument
      newArr.push(arr.slice(i, i + size)); // slice and push chunks to our new arr 
    }
  } return newArr; //returns our array with chunks
}

I looked through other peoples solutions as well as the hints and didn’t find this answer anywhere.
This solution will pass the test but is this a good solution?


#2

If it passes and you thought it out, you’re good :+1:. The code is also pretty clear.

Since it is known that the the increment is fixed, you could directly increment by size and let go of checking whether i is a multiple of size.

for (var i = 0; i < arr.length; i += size) {
  newArr.push(...);
}