0

Problem with the "OR" operator

For some reason, this code only check for the first condition in the if statement and leaves the rest. Any ideas why? https://code.sololearn.com/Wr7VaKsq82Y1/?ref=app

28th Dec 2019, 9:50 PM
SolomonDavid Akesoba
SolomonDavid Akesoba - avatar
4 odpowiedzi
+ 1
The long way to do this would be to say if(val.includes("1") || val.includes("2") || ...) Regex would be your friend here as it can do this in a very short line demonstrated here. https://code.sololearn.com/W2SE6hkZiBSz/?ref=app
28th Dec 2019, 10:09 PM
Russ
Russ - avatar
+ 3
If you try this line, you'll see that the result is "1". alert("1"||"2"||"3"||"4"||"5"); So, this line in your code: if(val.includes("1"||"2"||"3"||"4"||"5")){alert("yay") is the same thing as having: if(val.includes("1")){alert("yay")
28th Dec 2019, 10:01 PM
r1c5
r1c5 - avatar
+ 2
I can't give you an answer, but I will suggest for you to use regex and match instead. I will link you to an external website which explains both, but I also think SoloLearn has an explanation too. You can learn it in here: https://www.w3schools.com/jsref/jsref_match.asp
28th Dec 2019, 10:01 PM
SoundInfinity
SoundInfinity - avatar
+ 1
Thanks a lot, guys.
29th Dec 2019, 4:27 AM
SolomonDavid Akesoba
SolomonDavid Akesoba - avatar