JS - Almost done with algorithm