I can understand that the OA wants to test the elements one at a time in the array, then append new elements to the array (to also test) when particular conditions are met.
i have not verified this in js, but most languages I have used allow you to add new elements to the end of the array at any step of a for loop.
I have used the same process for linearizing data trees into arrays.