Updated JS with while loop.