Refactor JavaScript to readable condition