Anagrams algorithm implemented in js