0

I don't find what I need on internet, or may be I don't use correct words, but here is my problem.

I have a string for exemple: Ok bot, show me the doc of the Foo program.

And there is my keywords: ["bot","doc", "show", "Foo"]

I want that if the string contains 3 keyword or more, my function will return a message for exemple

I have think about

var message = "Ok bot, show me the doc of the Foo program.";
var keywords = ["bot","doc","show","foo"];
if(keywords.indexOf(message) >=3 ){
  console.log('ok I understand');
}

But it not works

Can someone help me please ?

Thank you

0

1 Answer 1

2

You are calling the indexOf function, which returns the items index in the array. In your case you are checking message in the array keywords which is logically wrong condition

You can filter the found keywords via Array#filter and String#includes and then check their length.

var message = "Ok bot, show me the doc of the Foo program.";
var keywords = ["bot","doc","show","foo"];

var keywordsFound = keywords.filter(item => message.includes(item));

if(keywordsFound.length >= 3 ) {
  console.log('ok I understand');
}

Sign up to request clarification or add additional context in comments.

2 Comments

you should edit your includes link to be this one: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
@MikeCheel Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.