Chunky monkey challenge - where does my logic go wrong? [SOLVED]

Hi everyone,

I made it through the basic algorithm problems up to Chunky Monkey, but now I’m a bit stuck and was just wondering if any of you might have an idea what is wrong with my code? It’s this:


function chunkArrayInGroups(arr, size) {
  var newArr = [];
  var n = 0;
  var m = size;
  var z = arr.length % size;
  
   while
     (arr.length >= m) {
     newArr.push(arr.slice(n, m));
     n = n + size;
     m = m + size;
    
   } if (z>0) {
     newArr.push(z);
   }
  
  return newArr;
}

This passes the first three tests, when the initial array can be evenly divided into chunks. My thought was that when the while loop exits, one should just tack on the remainder of (the initial array’s length divided by the chunking size) and return the new array with chunks plus remainder. But this is not what my machine is doing!

Any pointers would be most welcome! Thanks

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

Thanks! That looks much better now. Will remember the triple backticks for the future.

You gotta keep slicing the original array. All seems well until your if condition, which just pushes z, instead of slicing the last part of the original arr. If you want to see my solution I’ve hid it here:

[details=Summary]My solution:

function chunkArrayInGroups(arr, size) {
  // Break it up.
  var b = [];
  var c = arr.length / size;
  var d = Math.ceil(c);
  for ( var i = 0; i < d; i++ ) {
    var a = arr.slice(i * size, size + i * size);
    b.push(a);
  }
  return b;
}

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

[/details]

A few things:

  • General advice: use meaningful variable names. n? m? z? Writing and (especially) reading code is much easier when the variables make sense.
  • Your solution isn’t working when the length of arr is not divisible by size.
  • The following doesn’t make sense.
} if (z>0) {
     newArr.push(z);
   }

z is the remainder of arr's length divided by size, so this code translates to "if the length of arr is not divisible by size, then add the remainder of arr.length divided by size to the array. See why good variable naming is important?

1 Like

Yes meaningful variables are helpful. Sometimes I’m lazy with short codes and just use a,b,c… but good names are better for others to read.

Thanks, your feedback helped me fix the mistake. Here’s the new version, hopefully with better variable names:

function chunkArrayInGroups(arr, size) {
  var newArr = [];
  var counter1 = 0;
  var counter2 = size;
  var remainder = arr.length % size;
  var end = arr.length - remainder;
  
   while
     (arr.length >= counter2) {
     newArr.push(arr.slice(counter1, counter2));
     counter1 = counter1 + size;
     counter2 = counter2 + size;
    
   } if (remainder > 0) {
     newArr.push(arr.slice(end, end + remainder) );
   }
  
  return newArr;
}

Good job!
(I’m going to mark this post as “solved”)