Chunky Monkey Help Needed

Chunky Monkey Help Needed
0

#1

Wonders if someone can help with this. The solution I have come up with is my attempt to solve this without slice(). I think it can be done, and I think I am close to solving it. Don’t pay any attention to the third for loop, as it is written wrong and will be changed. My question is this: why are the first two for loops pushing into bigArray twice each? Im sure its something dumb that is sitting there right in front of me. inb4 there are easier ways to solve this

Your code so far

function chunkArrayInGroups(arr, size) {
  // Break it up.
  var sliced = [];
  var remains = [];
  var otherRemains = [];
  var bigArray = [];
  var moreRemains = (size * 2);
  for (var i = 0; i < size; i++) {
    sliced.push(arr[i]);
    bigArray.push(sliced);
  }
  for (var j = size; j < (size * 2); j++) {
    remains.push(arr[j]);
    bigArray.push(remains);
    
  }
  
  if (arr.length > 4 || size <=3) {
    
  for (var x = moreRemains; x < arr.length; x++); {
    otherRemains.push(arr[x]);
    bigArray.push(otherRemains);
  }
  }
  
  
  
  
  return bigArray;
}


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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:54.0) Gecko/20100101 Firefox/54.0.1 Waterfox/54.0.1.

Link to the challenge:


#2

Sorry I meant my IF statement is written wrong, not the third for loop.


#3

I modified your code to contain only the parts which work with the first loop. I added some console.log() statements so you could see what is happening during the execution of the code.

See if this helps you visualize what is going on. Once it loads, click the Run button at the top of the code panel to see the results on the right.


#4

Thanks! I figured it out! The reason it was happening twice is because I had the bigArray.push() INSIDE the for loops and it was being iterated by the loops! Dumbass mistake, but if I’m going to make a mistake, I’d rather do something stupid than totally not understand it!!