Skip to content Skip to sidebar Skip to footer

.includes() Checking For Keywords In Prompt()

I'm creating a sort of chatbot which will run on imbedded keywords stored in arrays, in this example I have array x being checked in y. This returns true whenever I exactly type He

Solution 1:

You would need to either check for each word, or use a Regular Expression like in this snippet

var x = ['Hello', 'Hi', 'Sup'];
var y = prompt("Looking for a Hello...");

var containsX = x.some(word=>y.includes(word))

if (containsX){
    alert("You Said Hello!");
} else {
    alert("No Hello Found!");
}

Solution 2:

Try to use indexof.

As mdn says:

The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.

let x = ['Hello', 'Hi', 'Sup'];
let y = "Looking for a Hello...";

console.log(x.some(s=> y.indexOf(s)));

Post a Comment for ".includes() Checking For Keywords In Prompt()"