I am pretty sure I am missing something basic here but I am having trouble with using multiple or
or ||
operators with my if statement.
For some reason the if statement is not catching the name variable:
JavaScript
x
11
11
1
testword = "billy"
2
3
if ((testword != "billy") ||
4
(testword != "tom") ||
5
(testword != "sara") ||
6
(testword != "michael")) {
7
console.log("none of the names match")
8
} else {
9
console.log("name found!")
10
}
11
When I try this I get none of the names match
when I should get name found!
Advertisement
Answer
Your logic is a bit convoluted
A far simpler approach to both write and understand is put all those names in an array and see if the array includes the testword. This is only a single boolean test
JavaScript
1
8
1
const testword = "billy",
2
words = ["billy", "tom", "sara", "michael"]
3
4
if (words.includes(testword)) {
5
console.log("name found!")
6
} else {
7
console.log("none of the names match")
8
}