I'm doing this little exercise where I'm trying to count the amount of word
occurrences in my string 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);
}
My function is as follows
findWord(/fox/i, theSentence);
My expected output was supposed to be fox: 2
But my actual out puts was /fox/i: 0
Any pointers on what I might be doing wrong?
My goal is to repeat this function so it reads the
, fox
, and brown
Any pointers? Thank You