Problem 206 solution in JavaScript