DEV Community

Discussion on: JS Coding Challenge: Find Anagrams

Collapse
 
bugb profile image
bugb • Edited
const words = ['mountain', 'anatomy', 'anemic', 'boldness', 'cinema', 
'iceman', 'machine', 'mechanic', 'elbow', 'below', 'state', 'taste', 
'dusty', 'night', 'study', 'thing', 'search', 'arches', 'chaser', 
'animal', 'manila', 'icewoman'];
const rearrange = text => [...text].sort()+''
const findAnagrams = (word, allWords) => {
   const rs = [];
   const rearrangeWord = rearrange(word);
   for (const wordItem of allWords) {
       if (wordItem.length === word.length) {
         if (rearrange(wordItem) === rearrangeWord) rs.push(wordItem);
       }
   }

  return rs;
};
};
Collapse
 
bugb profile image
bugb • Edited

We can do something like that, since length is a property of an array and it does not cost anything so we can compare both 2 words's length and then use compare function