Chunky Monkey please help

Tell us what’s happening:
Hello everyone here. I thought of this question for 2 days and couldn’t solve it, and then I read the solution but I still couldn’t understand the logic. Perhaps I have some misunderstanding about “for loop” or "if"
I am going to add my question after the sentence, and hope you guys can help me with my confusion.

Your code so far

function chunkArrayInGroups(arr, size) {
var temp = [];
  var result = [];

  for (var a = 0; a < arr.length; a++) {
    if (a % size !== size - 1) // could anyone clarify this condition for me, I am really confused. My confusion is that if this condition stop the unqualified a and pass qualified a to next sentence? if so, the output of next sentence will not be [0.1.2.3.4.5.6.7.8]
      temp.push(arr[a]); // This action will make the temp [0,1,2,3,4,5,6,7,8], because a < arr.length so a = 8. is it correct?
    else {
      temp.push(arr[a]);
      result.push(temp);
      temp = [];
    }
  }

  if (temp.length !== 0)
    result.push(temp); // I still don't understand how those sentence make it happen. How they make the designated number in one array, and make designated number of array in an array. Could anyone possibly clarify it for me? Thanks a lot in advance !
  return result;
}
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 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/65.0.3325.162 Safari/537.36```.

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

Try running the code through http://pythontutor.com/ and see if you understand it better.
You can step through the code line by line with it. :sunglasses:

Regarding: if (a % size !== size - 1)
This is a test to check the last group. eg) if the array is 8 and the group size is 3 then the last group will only be 2 (I think). I did it another way.

Hello JohnnyBizzel,
Thank you so much for your kindest help.
This website is super useful.
Once again, thank you very much!

1 Like