Here, we receive array and the size where size = group size (we will group the 2 consecutive array elements in array of size 2)
initializing empty array to store the grouped arrays.
This For loop will iterate through the contents of array while a < array’s length.
Therefore, in this case, it will iterate while a < 4 ;
Now, increment statement => a+=size i.e. a=a+size , therefore, in this case, it will add 2 to each iteration that means a=0 then a=2 then a=4 but condition statement says iterate only if a<4 so this loop will only iterate 2 times when a=0 and a=2.
Here we are using array’s slice method to slice array.
1st iteration=> when a = 0 ;
( a = 0 => starting index, a+size => 2 ending index (exclusive) );
gather = [“a”,“b”]
2nd iteration=> when a = 2;
( a = 2 => starting index, a+size => 2+2 = 4 ending index (exclusive) );
gather = [“c”,“d”]
Here we are pushing each sliced gather array into emptyArr array.
Note: push is an array method to add new element at the end of array.
Here we are returning the emptyArr (Grouped Array) .
Here we are calling the function that we just created.
[“a”, “b”, “c”, “d”] is an array as an first argument, 2 = size (second argument)
if you console the output of this call.
output => [[“a”, “b”], [“c”, “d”]]
This Array has only 2 elements and each element is now an array of grouped elements.
It definitely takes some time to remember/get used of coding techniques/concepts.
Some things take more time and its all about muscle memory. More you try , the more you remember.
Keep on coding and practicing and definitely ask questions where u get stuck.