cd-4 js answer refactored