rewrite collocation javascript code