2

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

Squirtle
  • 21
  • 2

5 Answers5

2

You have several syntax problems in your code

  • /fox/i === arr[i] will never turns out to be true. because /fox/i is regex and arr[i] is having primitive strings.
  • return statement before count dosen't make any sense.
  • You're splitting theSentence outside of function and storing in arr, but in your function call you're passing theSentence only where it should be arr or use split inside function

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";

function findWord(word, arr){
    arr= theSentence.split(' ');
    let count = 0;
    for(let i = 0; i < arr.length; i++){
        if(word.test(arr[i])){
            count++;
        }
    }
    return console.log(word +": " + count);
}

findWord(/fox/i, theSentence);
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • I'm testing it on node and the `/gi` reg expression flag wasn't rendering properly, but your correction works when I use the '/i' regex. Why did you at the `/g` to the reg expressions? Thanks for the help! – Squirtle Mar 10 '19 at 06:52
  • @Squirtle `g` is a global flag used when you need to match more than one occurrences. but in this case it is not needed, my bad i placed it mistakenly,i already removed it – Code Maniac Mar 10 '19 at 06:53
2

There are few problems:

  1. You are using return count++ which will stop the further execution of function.Should remove return
  2. You are comparing RegExp() to a string instead you should use test() method of RegExp()
  3. You should return console.log(...) it will return undefined. You can return the value and then log it.

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 fox";
let arr= theSentence.split(' ');
function findWord(word, arr){
    let count = 0;
    for(let i = 0; i < arr.length; i++){
        if(word.test(arr[i])){
            count++;
        }
    }
    return word +": " + count
}
console.log(findWord(/fox/i, arr));

A better way to that is using String.prototype.match() and g flag in Regex. If you want to match the whole word fox use /fox ?/gi as regex. If you want to match it from inside word use /fox/gi

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";
function findWord(word, str){
    return word + ": " + str.match(word).length
}
console.log(findWord(/fox/gi, theSentence));
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
1

Try this. Use filter to search the word in the array and return the count

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;
    count=arr.filter(e=>e.toLowerCase()==word.toLowerCase()).length
    return word +": " + count;
}

console.log(findWord('the',arr))
ellipsis
  • 12,049
  • 2
  • 17
  • 33
1

The second parameter is named arr, which sounds like you meant to pass an array - if that's what you wanted, pass arr instead of theSentence:

/fox/i is a regular expression, which is an object, not a primitive - it will never be === to anything else. Call .test on each word in the array, on the regular expression, and increment count with count++, and don't return inside the loop - if you return, the function will terminate immediately. (you want it to iterate over all words in the array)

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(word.test(arr[i])){
            count++;
        }
    }
    return console.log(word +": " + count);
}
findWord(/fox/i, arr); 

You might consider using the pattern

/\bfox\b/i

instead - this will match the number of times fox exists in the input, case-insensitive, with a word boundary on each side.

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";

function findWord(pattern, sentence){
  const count = sentence.match(pattern).length;
  return console.log(pattern +": " + count);
}
findWord(/\bfox\b/gi, theSentence);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

You are passing Regex so use with pattern.test(str) .And i could rewrite code with simple format

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";


function findWord(word, arr){
   arr= arr.split(' ');
   return arr.filter(a=> word.test(a)).length
}

console.log(findWord(/fox/i, theSentence));
console.log(findWord(/The/i, theSentence));
prasanth
  • 22,145
  • 4
  • 29
  • 53