Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
sum by reduce solution in Clear category for Popular Words by Sim0000
function popularWords(text: string, words: string[]) {
const wordList = text.split(/\s+/).map(w => w.toLowerCase())
let result = {};
for(const word of words){
result[word] = wordList.reduce((sum, w) => sum + Number(w == word), 0);
}
return result;
}
console.log('Example:')
console.log(popularWords(`
When I was One
I had just begun
When I was Two
I was nearly new`, ['i', 'was', 'three', 'near']))
April 24, 2020
Comments: