Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda