Last active
August 29, 2015 14:17
-
-
Save ericroberts/49fd3c3e68bf7d31b872 to your computer and use it in GitHub Desktop.
Doing the anagram exercise from exercism.io
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
var anagram = function(word) { | |
return new Anagram(word); | |
}; | |
function Anagram(word) { | |
this.matches = matches; | |
function matches() { | |
possible_anagrams = retrieveAnagrams(arguments); | |
return possible_anagrams.filter(function(anagram) { | |
return notSameAs(anagram) && normalize(word) === normalize(anagram); | |
}); | |
}; | |
function retrieveAnagrams(args) { | |
if(args[0] instanceof Array) return args[0]; | |
return [].map.call(args, function(anagram) { | |
return anagram; | |
}); | |
} | |
function notSameAs(anagram) { | |
return word.toLowerCase() !== anagram.toLowerCase(); | |
} | |
function normalize(text) { | |
return text.toLowerCase().split("").sort().join(""); | |
} | |
} | |
module.exports = anagram; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment