Implementing common kata in Javascript