DEV Community

Discussion on: Scanning user input and looking for keywords

Collapse
 
paxfeline profile image
David Newberry • Edited

I think what you need is a boolean flag. It would look something like this:

let noOptions = true;
for (i = 0; i < options.length; i++){
    if(text.search(options[i]) != -1){
        noOptions = false;
        ...
    }
}

and then after the loop you put a conditional:

if ( noOptions ){
    // no options matched
}

That conditional would replace the part inside the loop that starts with

else if (i == options.length-1)
Collapse
 
kevinuulong profile image
Kevin Long

I just tried that and did a little more testing; it seems that when I load the next json file as an object it ends up checking both of the two seperate files at once. Do you have any ideas as to how to fix that?