Tengo una matriz llamada ' hola ' que contiene expresiones
var hello = ['hi', 'hello', 'yo', 'hey', 'howdy'];Y una cadena almacenada en ' usertext ' que obtengo del usuario.
Quiero verificar si ' usertext ' contiene una de las expresiones almacenadas en ' hello ' y devolver verdadero/falso , ' usertext ' puede contener una oración completa.
Hasta ahora lo intenté
if (usertext.includes(hello)) { var garfieldtext = "Hello. I'm Garfield, what's your name?"; }Lo siento si ya se ha preguntado, no encontré (ni entendí) la solución por ahí.
soy muy nuevo en JS
hacer uso de some método de JavaScript. Devuelve true si se cumple alguna de las condiciones.
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?"); }documentación para algunos