0

I have an array called 'hello' containing expressions

var hello = ['hi', 'hello', 'yo', 'hey', 'howdy'];
        

And a string stored in 'usertext' that I get from the user.

I want to check if 'usertext' is containing one of the expressions stored in 'hello' and return true/false, 'usertext' can contain a whole sentence.

So far I tried

if (usertext.includes(hello)) {
    var garfieldtext = "Hello. I'm Garfield, what's your name?";
}

I'm sorry if it has already been asked, I didn't find (or understand) the solution out there.

I'm very new to JS

1 Answers1

0

make use of JavaScript's some method. It returns true if any of the condition is met.

const hello = ['hi', 'hello', 'yo', 'hey', 'howdy'];
const userText = "some text in here hello";

const helloExist = hello.some(item => userText.toLowerCase().includes(item));

if (helloExist) {
    console.log("Hello. I'm Garfield, what's your name?");
}

documentation for some

ahsan
  • 1,409
  • 8
  • 11