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
4 Answers
+ 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
+ 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")
+ 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
+ 1
Thanks a lot, guys.