2019-03-10 Daily Challenge
What I've done today is Where my anagrams at? in JavaScript.
CodeWars
Problem
Where my anagrams at?
What is an anagram? Well, two words are anagrams of each other if they both contain the same letters. For example:
'abba' & 'baab' == true
'abba' & 'bbaa' == true
'abba' & 'abbba' == false
'abba' & 'abca' == false
Write a function that will find all the anagrams of a word from a list. You will be given two inputs a word and an array with words. You should return an array of all the anagrams or an empty array if there are none. For example:
anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada']) => ['aabb', 'bbaa']
anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer']) => ['carer', 'racer']
anagrams('laser', ['lazing', 'lazy', 'lacer']) => []
Solution
function anagrams(word, words) {
return words.filter(w => word.split("").sort().join("")===w.split("").sort().join(""))
}
////
function anagrams(word, words) {
word = word.split("").sort().join("");
return words.filter(w => word===w.split("").sort().join(""))
}
////
String.prototype.sort = function() {
return this.split("").sort().join("");
};
function anagrams(word, words) {
return words.filter(x => x.sort() === word.sort());
}
////