I have three urls I have split down to keywords. In JavaScript I want to see if two of these Urls match two given keywords. Both keywords have to match two of the url's for the code to proceed but I can't think of a short way to do it.
I have tried...
if (url1 || url2 || url3 === "keyword1" && "keyword2") {
....
}
And
if (url1 || url2 || url3 === "keyword1" && url1 || url2 || url3 == "keyword2") {
....
}
This is as far as I got. The problem is, it returns a match for just one of the keywords instead of only returning a match for both . I have a feeling that not ruling out the first match is causing the second to match the same variable. I hope this makes sense!
Can anybody help me?
|| and && are logical operators. You can not use strings for their operands(meaningfully).
You will have to individually check every url match and do logical operation on the result. Also, you should use () to not mess with operator precedence.
Try something like:
[url1, url2, url3].indexOf("keyword1") != -1 && [url1, url2, url3].indexOf("keyword2") != -1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With