Chunky Monkey for loop question

I’m having trouble wrapping my head around how i is being counted in arr.slice. would i be 2 since its going by i+=size where size is the number in this scenario.

  **Your code so far**
function chunkArrayInGroups(arr, size) {
let total = [];
for (let i=0; i<arr.length; i+=size){
  total.push(arr.slice(i,i + size));
return total

console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));
  **Your browser information:**

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

Challenge: Chunky Monkey

Link to the challenge:

You can easily check required values by adding console.log(“i”, i) inside your loop

How to count variable i don’t depend on the value of size but the execution order of for loop. The steps are as follows:

  1. initialize i to zero
  2. i is less than the length of array “a”, “b”, “c”, “d”, push the arr.slice(0,0+2) which is [“a”, “b”] to total
  3. i = 0+2 and i is still less than 4, then push arr.slice(2,2+2) which is [“a”, “b”] to total
  4. i = 2+2 and i equals 4, quit the loop
  5. the final result should be [ [ “a”, “b”,], [ “c”, “d” ] ]

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.