Chunky Monkey Algorithm

Tell us what’s happening:
Hello everyone,
Another noob question… I cannot seem to get my loop to work properly. It keeps returning the first 2 numbers over and over. What am I doing wrong?

Thank you in advance for your assistance!

Your code so far

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

chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/chunky-monkey

I changed

result.push(arr.slice(i,size+i++));

Code above only works if the size is 2.

if i write:

result.push(arr.slice(i,size+size));

It messes with the first loop and doubles the size… I think I need to some how increment i+size on the 2nd loop and I cannot figure out the proper syntax.

Please let me know if i’m on the correct path.

Thank you again for all your help!!


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

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

You are very close. I was able to make your code work with three simple modifications. One of them you’ve already made. You’re very close to finishing another. I think you’re close to knowing what the third modification should be, but you’re not looking in the right place.

looks like in the for loop “i” does not always have to be “i++” or “i - -”. Thank you for the feedback looks like one more down :).

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

  result.push(arr.slice(i,size+i));
}
1 Like