我正在做这个小练习,试图计算word我的字符串中出现的次数theSentence。
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
任何指针?谢谢你