refactored optimized js prob 1