【发布时间】:2019-07-31 18:42:30
【问题描述】:
我正在做这个小练习,我试图计算我的字符串theSentence 中出现的word 的数量。
const theSentence = "The quick brown fox jumps over the lazy brown dog, who thinks the world is flat and the sky\'s blue. The brown fox then goes to sleep";
let arr= theSentence.split(' ');
function findWord(word, arr){
let count = 0;
for(let i = 0; i < arr.length; i++){
if(arr[i] === word){
return count++;
}
}
return console.log(word +": " + count);
}
我的功能如下
findWord(/fox/i, theSentence);
我的预期输出应该是fox: 2
但我的实际输出是/fox/i: 0
关于我可能做错了什么的任何指示?
我的目标是重复这个函数,使其显示为the、fox 和brown
有什么建议吗? 谢谢你
【问题讨论】:
标签: javascript arrays string search count