Hi,
Your loop logic is a little flawed. i < arr.length doesn’t seem to be a reasonable test for exit condition especially since i is increasing by one and arr.length is decreasing by whatever size is. Your loop is very likely to complete with a little piece of arr left over that still needs processing which you have tried to rectify with a if…else at the end but that doesn’t always work.
Try putting what you are doing into words.
Using the last test chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2) as example I might think something like this
Do I have array to process? [0, 1, 2, 3, 4, 5, 6, 7, 8] Yes!
Then take off two elements [0,1] and push onto newArr
Do I have array to process? [2, 3, 4, 5, 6, 7, 8] Yes.
Take off two more and push onto newArr
Do I have array to process? [4, 5, 6, 7, 8] Yes.
Take off two more and push
Do I have array to process? [6, 7, 8] Yes.
Take off two more and push
Do I have array to process? [ 8] Yes.
Take off two more and push (OK really just one)
Do I have array to process? [ ] No
Then I’m done!
return newArr