Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda