JavaScript solutions refactored