JavaScript loop converter