A solution to r/dailyprogrammer's challenge #186 [Special] in JavaScript