JavaScript homework refactoring