javascript - my isStopWord function thinks "cat" and "catnip" are the same words -


i'm trying write simple function takes in word , stopword see if same words. return true if are. far, doing this,

function isstopword(word, stopwords) {   return (stopwords.indexof(word) !== -1); }  console.log(isstopword("cat", "cat")); 

returns true, doing this

console.log(isstopword("cat", "catnip"); 

also returns true... now, don't think know enough how ".indexof" works figure out why returns true in both cases. can me fix function knows if it's same word , not first 3 letters? because doing this,

console.log(isstopword("catnip", "cat"); 

returns false, i'm little bit confused. thanks!

the string.prototype.indexof() method returns position of first occurrence of specified value in string. cat has 1 occurence inside catnip index returned !== 1

if want check 1 word one. use following snippet.

function isstopword(word, stopwords) {    return stopwords === word;  }    console.log(isstopword("cat", "cat"));  console.log(isstopword("catnip", "cat"));

if want see if word present inside array of words, use following snipper using array.prototype.indexof()

function isstopword(word, stopwords) {    return stopwords.indexof(word) !== -1;  }    console.log(isstopword("cat", ["cat", "dog", "bird"]));  console.log(isstopword("catnip", ["cat", "dog", "bird"]));


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -