jscript loop refactor