Codility Demo Test Solution Javascript