Added another JavaScript solution

But this one keeps no internal state. It's purely functional.