Solution 2 in JS