Hi i wonder if someone can explain Array.prototype.reduce.call
is it:
- convert to Array
- invoke it in array format (call)?
Or am i missing something?
thanks
//countLetters function that is called twice (for the word and the currentword)
const countLetters = word =>
Array.prototype.reduce.call(
word,
(counts, letter) => {
letter = letter.toLowerCase();
counts[letter] = counts[letter] || 0;
counts[letter]++;
return counts;
},
{}
);
// k
const looseEqual = (obj1, obj2) => {
const obj1Keys = Object.keys(obj1);
const obj2Keys = Object.keys(obj2);
if (obj1Keys.length != obj2Keys.length) {
return false;
}
return obj1Keys.every(key => obj1[key] === obj2[key]);
};
//looseEqual(letterCount, countLetters(currentword)):
//countLetters(currentWord) will be called first since its within the function
function anagrams(word, words) {
const letterCount = countLetters(word);
return words.filter(currentWord =>
looseEqual(letterCount, countLetters(currentWord))
);
}
anagrams("abba", ["aabb", "abcd", "bbaa", "dada"]);