let q;
while ((q !== "yes")||(q !== "no")) {
q = prompt("yes or no?");
}
I've tried this and I couldn't understand why it wouldn't work since this:
while (q !== "yes") {
q = prompt("yes or no?");
}
works.
let q;
while ((q !== "yes")||(q !== "no")) {
q = prompt("yes or no?");
}
I've tried this and I couldn't understand why it wouldn't work since this:
while (q !== "yes") {
q = prompt("yes or no?");
}
works.
Uh, it seems that you used
while ((q !== "yes")||(q !== "no"))
This will always translate to true
since q cannot be both "yes" and "no", it will always evaluate to true. The condition should be should be
while ((q !== "yes") && (q !== "no"))
The expression
(q !== "yes")||(q !== "no")
will always be be truthy, because q
can't be both yes
and no
at the same time. If either condition is fulfilled, that while
will be truthy, and the loop will continue.
Use &&
instead:
(q !== "yes") && (q !== "no")
Or, even more readably, use .includes
:
while (!['yes', 'no'].includes(q)) {
let q;
while (!['yes', 'no'].includes(q)) {
q = prompt("yes or no?");
}