Implement the Chunky Monkey Algorithm

Hello folks! I hope you’re doing well. Now that I passed the test, i’d like you to give a look into my code and give any feedback!

Implement the Chunky Monkey Algorithm

function chunkArrayInGroups(arr, num) {
  let arrayLength = arr.length;
  const resultArray = [];
  
  while (arrayLength >= num) {
    const subArr = [];
    for (let i = 0; i < num; i++) {
      subArr.push(arr.shift());
    }
    arrayLength -= num;
    resultArray.push(subArr);
  }

  if (arrayLength) {
    resultArray.push(arr.slice(-arrayLength));
  } 

  return resultArray;
}

Before writing feedback I’d like to ask few questions, partially to give something to self-reflect on.

What are you thinking about it at this point? Was there something especially hard, or easy? Is there anything specific that you’d like to improve in the solution?

Thank you for your attention.

Even tough I passed the test, I still have the feeling it can be optimized in readness and computation and structure.

For example, subArr. I wouldn’t have created this if Array.shift() could remove multiple elements at once. Likewise Array.shift() returns, I could push directly into resultArray.

To be honest, it’s not necessarily needed at all to remove items from original array.


  while (arrayLength >= num) {

Because arrayLength is in condition of the while loop, this variable needs to be additionally manually updated within loop. In this place, the actual length of the array could be used instead. Which would be updated on it own.


  if (arrayLength) {
    resultArray.push(arr.slice(-arrayLength));
  } 

At this point in the code anything that’s in the arr needs to be added to the resultArray. This makes -arrayLength argument obsolete. Another thing that’s sticking out is usage of the slice method. This is inconsistent with how previous chunks were handled - keeping it in the original array vs removing.

Something to consider - what would be needed to remove this check of the length, and instead do all the splitting to chunks in the while loop?


Using shift method. This isn’t mistake per se, but something that needs to be kept in mind. Mutating arr makes this function to cause side-effects. In other words, after passing array to chunkArrayInGroups, it will be modified. Depending on requirements that might not be desired (in this case there are none).

Second caveat of shift is depending on the internal implementation (or how it is in different programming languages), it might be relatively slow. This is after all removing first item from the array, which usually means that whole array might need to be copied over. To be clear - any slowdown caused by this won’t be noticed unless it’s for much, much bigger arrays than used in this challenge.


Naming of subArr variable. This is more nit-picking. It’s actually ok-ish, but could be improved, to make much clearer what it contains. There’s at least two words in the name of function, which could help with that.

1 Like

I need to pass some time on it before digesting but thank you @sanity for detailed explanation! :folded_hands:

1 Like