-2

JS Guess the number game. Everything checks out but the code get stuck in loop while (go!="yes" || go!="no" ) even input was yes or no. What did I miss?

// Ask user to retry
while (guess != random) {
    let go = prompt("wrong guess -_-\nWould you like to retey? (yes/no)");
    console.log(go)
    while (go!="yes" || go!="no" ) {
        go = prompt("You must enter yes or no\nWould you like to retey? (yes/no)");
        console.log(go,8)
    }
    if (go == "no") {
        break;
    }
    guess = parseInt(prompt("Enter your guess"));
}
Ocean
  • 19
  • 2
  • 2
    When `go = "no"` it is not "yes", therefore the condition passes. When `go = "yes"` it is not "no", therefore the condition passes. When `go = "anything else"` it is not "yes", therefore the condition passes. – VLAZ Jul 05 '22 at 18:43

1 Answers1

1

OK, given your expression.

go!="yes" || go!="no"

Let's say go is wibble

true || true == true

Now let's say go is "yes":

false || true == true

Now let's say go is "no":

true || false == true

There is no condition where that doesn't rock out as true.


You need to use && here.

Or, you can use the includes method of an array which is easier to understand when eyeballing the code and even more convenient when you have a longer list of values to test:

if ( !["yes", "no"].includes(go) ) 
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335