简体   繁体   中英

how to find if a string includes something in a switch() javascript

i would like to find out if a string contains a certain word using a switch() statement

here is an example of what i want to use that for:

let text = "among"

switch(text.toLowerCase().includes()){
   case "among"
      console.log("not funny")
      break

   default:
      break
}

Switch statement evaluates an expression so this code will not work. You could store possible inclusions in an array and loop through the array, then depending on the evaluation output a certain response with a switch statement.

let text = "among"

let words = ["among", "text", "test"]


function test(word, wordArr) {
    for (let w of wordArr) {
        if (w === word) {
            switch (w) {
                case "among":
                    console.log("among word");
                    break;
                default:
                    break;
            }
        }
    }
}

console.log(test(text, words))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM