whereMyAnagramsAt

title

What is an anagram? Well, two words are anagrams of each other if they both contain the same letters. For example:

1
2
3
4
5
'abba' & 'baab' == true

'abba' & 'bbaa' == true

'abba' & 'abbba' == false

example

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:

1
2
3
4
5
anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada']) => ['aabb', 'bbaa']

anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer']) => ['carer', 'racer']

anagrams('laser', ['lazing', 'lazy', 'lacer']) => []


My solution

1
2
3
4
5
6
7
8
9
10
11
function anagrams(word, words) {
var result = [];
word = word.split('').sort().join('');
words.map(function(v){
if(word == v.split('').sort().join('')){
result.push(v);
}
})

return result;
}

good solution

one

1
2
3
4
function anagrams(word, words) {
word = word.split('').sort().join('');
return words.filter(function(v) {return word == v.split('').sort().join('');});
}