问答题902/1593如何查找一篇英文文章中出现频率最高的单词?

难度:
2021-11-17 创建

参考答案:

1function findMostWord(article) { 2 // 合法性判断 3 if (!article) return; 4 5 // 参数处理 6 article = article.trim().toLowerCase(); 7 8 let wordList = article.match(/[a-z]+/g), 9 visited = [], 10 maxNum = 0, 11 maxWord = ""; 12 13 article = " " + wordList.join(" ") + " "; 14 15 // 遍历判断单词出现次数 16 wordList.forEach(function(item) { 17 if (visited.indexOf(item) < 0) { 18 let word = new RegExp(" " + item + " ", "g"), 19 num = article.match(word).length; 20 21 if (num > maxNum) { 22 maxNum = num; 23 maxWord = item; 24 } 25 } 26 }); 27 28 return maxWord + " " + maxNum; 29}

最近更新时间:2021-11-17

赞赏支持

预览

题库维护不易,您的支持就是我们最大的动力!