'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
}
Solution 1:[1]
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))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sean Lawton |
