JavaScript - Conditionals and loops