Just wondering, is there a way to add multiple conditions to a .includes method, for example:
JavaScript
x
2
1
var value = str.includes("hello", "hi", "howdy");
2
Imagine the comma states “or”.
It’s asking now if the string contains hello, hi or howdy. So only if one, and only one of the conditions is true.
Is there a method of doing that?
Advertisement
Answer
That should work even if one, and only one of the conditions is true :
JavaScript
1
13
13
1
var str = "bonjour le monde vive le javascript";
2
var arr = ['bonjour','europe', 'c++'];
3
4
function contains(target, pattern){
5
var value = 0;
6
pattern.forEach(function(word){
7
value = value + target.includes(word);
8
});
9
return (value === 1)
10
}
11
12
console.log(contains(str, arr));
13