Interesting problem! Here's my attempt of solving it in more functional programming style, without converting it into a convoluted one-liner. codepen.io/dabrorius/pen/MdPXxB
Scratch that! There's a simpler implementation: codepen.io/dabrorius/pen/xNyJMO
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.