-2

I have two arrays.

First one: const triggerWord = ["what", "how", "where", "who", "can"];

Second one: const myArray = query.split(" "); - it is created after splitting the user input in this case called query.

One is a fixed number of strings, that will not change as they are the trigger words. The other array is an input string that has been split into an array, separated by a space.

I am trying to build some sort of control flow that if any of the triggerWords are present within the myArray then it should call a specific function call the questionFunction(). However the else is that is should call the statement function. Here are the functions I have attempted:

#1: Switch Case:

switch (lowercaseMyArray.includes()) {
    case triggerWord[0].toString():
        questionFunction(lowercaseMyArray);
        break;
    case triggerWord[1].toString():
        questionFunction(lowercaseMyArray);
        break;
    case triggerWord[2].toString():
        questionFunction(lowercaseMyArray);
        break;
    case triggerWord[3].toString():
        questionFunction(lowercaseMyArray);
        break;
    default:
       statementFunction(lowercaseMyArray);

#2: If statement

if (lowercaseMyArray.includes(triggerWord[0] || triggerWord[1] || triggerWord[2] || triggerWord[3], 2)) {
    //alert("This is a question");
    questionFunction(lowercaseMyArray);
    //passe onto questionFunction
} else {
    statementFunction(lowercaseMyArray);
    //alert(myArray);
}

#3: Bool and If Statment

var lowercaseMyArrayBool = lowercaseMyArray.includes(triggerWord);

    if (lowercaseMyArrayBool === true) {
        questionFunction(lowercaseMyArray);
    } else {
        statementFunction(lowercaseMyArray);
    }

The idea being that if it contains a trigger word then the input must be a question and therefore treated as such. However if it doesn't contain the trigger words then the input is a statement and must be treated as that.

Any help or pointers will be greatly appreciated.

onaikepo
  • 47
  • 4
  • 1
    See [How can I find matching values in two arrays?](https://stackoverflow.com/q/12433604/1048572) and [Simplest code for array intersection in javascript](https://stackoverflow.com/q/1885557/1048572), but someone might be able to explain in detail why your approaches didn't work – Bergi Jan 16 '23 at 21:32
  • the first two approaches are bad because what if you want to add trigger words you must alter the code, the third approach seems fine, since expanding the list wont require changes to the code. – coding_potato Jan 16 '23 at 21:39

1 Answers1

-1

Resolved this with the following code:

const intersection = triggerWord.filter(element => lowercaseMyArray.includes(element)).length;
const intersectionBool = Boolean(intersection);

I just needed to cast the filtered array length as a Boolean, to get an accurate true or false result.

onaikepo
  • 47
  • 4
  • 1
    If you want a boolean just use [`some()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) instead of filter, also exits on first match for more efficiency. – pilchard Jan 16 '23 at 22:09
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 18 '23 at 04:07