A solution to r/dailyprogrammer's challenge #159 in JavaScript