Created
January 17, 2014 20:37
-
-
Save drueck/8480949 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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