A solution to r/dailyprogrammer's challenge #173 [Intermediate] in JavaScript