Chunky Monkey Algorithm

Chunky Monkey Algorithm
0

#1

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:


#2

The line above is always going to start slicing arr from index zero and include the elements up (but not including) the index size. If size is 2, this line will push an array containing only the first two elements into result. It will do this the same number of times as the number of elements in arr.

You need to think about how the first and second parameters of the slice function need to change so you end up with the correct slices going into the result array.


#3

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);


#4

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.


#5

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));
}