Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
Use an array to find a specific word in a string (JavaScript)

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!