refactoring JS1 arrays and loops