Skip to content

Instantly share code, notes, and snippets.

@drueck
Created January 17, 2014 20:37
Show Gist options
  • Save drueck/8480949 to your computer and use it in GitHub Desktop.
Save drueck/8480949 to your computer and use it in GitHub Desktop.
function Anagram(word) {
this.word = word;
}
Anagram.prototype.match = function(words) {
return words.filter(function(word) {
return areAnagrams(this.word, word);
}, this);
};
function areAnagrams(word1, word2) {
word1 = word1.toLowerCase();
word2 = word2.toLowerCase();
return word1 !== word2 && haveSameLetters(word1, word2);
}
function haveSameLetters(word1, word2) {
return sorted(word1) == sorted(word2);
}
function sorted(str) {
return str.split('').sort().join('');
}
module.exports = Anagram;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment