0

I'm working on a chatbot in Javascript, and I have a variable called "target", which is the variable that will contain what the user says in chat, and an array called "doingGood":

let doingGood = ["good", "great", "amazing", "awesome"];

What I want to do, is I want to make an if statement that will do something if the target variable is equal to a string that's in the doingGood array. For example, if target is equal to good, great, amazing, or awesome, do something. I've looked around on the internet, but I'm stumped and can't seem to find an answer. Help would be appreciated, thanks!

EDIT: The question is the same, but I tried the methods that they suggested there and they didn't seem to work. When I tried if (variable.target === doingGood), it gave me an error saying "variable is not defined."

  • 1
    `doingGood.indexOf(search) > -1`. ES6 version, `doingGood.includes(search)` – Rajesh Oct 21 '17 at 13:31
  • `doingGood.includes(myString)` – Andrew Li Oct 21 '17 at 13:32
  • This question is different from the "duplicate." This question is asking how to use an array as a set. The other question asks how a function can tell the type of a given object---specifically, is it an array, or not an array. – Solomon Slow Oct 21 '17 at 19:25

1 Answers1

0

you can use indexOf methd of array https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

if(doingGood.indexOf(target) !== -1){

// do something

}

  • Just a pointer. Its bad to answer a duplicate. If you know the solution and if it is very common, there is a high possibility that it has been asked before. Since you know the issue you are more likely to find it. So you should look for such post and paste it as comment (*until you do not have enough rep to close yourself*). – Rajesh Oct 21 '17 at 14:17