1
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.

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • 1
    `(q !== "yes")||(q !== "no")` will always evaluate to true since `q` can only be one thing. It'll either not be yes or not be no, or possibly not be either. – Nick Oct 21 '19 at 01:12
  • Possible duplicate of [Why non-equality check of one variable against many values always returns true?](https://stackoverflow.com/questions/26337003/why-non-equality-check-of-one-variable-against-many-values-always-returns-true) – Sebastian Simon Oct 21 '19 at 17:29

2 Answers2

1

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"))
0

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?");
}
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320