>>107115634
functional Javascript is ugly because the syntax was made for Java-style OOP, and so were most of the built-in object methods.
Picrelated is what that algorithm looks like when you use a syntax more amenable to FP.
Mind you, the algo is vulnerable to stack-overflow for deeply nested functions.
This is how flatten is implemented in Clojure:
(defn flatten
"Takes any nested combination of sequential things (lists, vectors,
etc.) and returns their contents as a single, flat lazy sequence.
(flatten nil) returns an empty sequence."
[x]
(filter (complement sequential?)
(rest (tree-seq sequential? seq x))))
>>107116477
you didn't get the point of the code in OP