Functional Javascript code