0

My code is getting into infinite loop even if I input "yes" or "yeah".

Below is my code:

var answer = prompt("Are we there yet?");   
while(answer != "yes" || answer != "yeah" ){
    var answer = prompt("Are we there yet?");    
}    
alert("YAY! We made it!!"); 
7
  • What is your quesion? Commented Jun 3, 2020 at 8:35
  • 2
    Use && instead of ||. Commented Jun 3, 2020 at 8:36
  • 1
    The answer will always be either not “yes” or not “yeah”. You want &&, not ||. Commented Jun 3, 2020 at 8:36
  • When answer == "yes" then it's not equal to "yeah". When answer == "yeah" then it's not equal to "yes" Commented Jun 3, 2020 at 8:37
  • Suppose you type "yes". Then your condition is "if false or true try again". If you type yeah, it's "if true or false try again". If you type anything else, it's "if true or true try again". There is no scenario in which your code doesn't try again. Commented Jun 3, 2020 at 8:37

2 Answers 2

1

Change || to &&.

You are asking: If your answer is not "yes" or it is not "yeah". If you answer with "yes", the answer is not "yeah". If you answer with "yeah" your answer is not "yes". That's why you haven an infinite loop.

Check out De Morgan's laws for more details. Negotiation of !(answer == "yes" || answer == "yeah") results also in changing the or operator.

var answer = prompt("Are we there yet?");

while(answer != "yes" && answer != "yeah" ){
    answer = prompt("Are we there yet?");    
}

alert("YAY! We made it!!"); 

Sign up to request clarification or add additional context in comments.

4 Comments

No need to define answer twice.
@SagiRika First, if you check the times, I started my answer before the answer was in the comments. Second check out SO Meta: Answer or comment: what's the etiquette?: "Answers should be answers". That is kind of obvious though... You cannot mark comments as the right answer.
I meant the fact that you use var answer twice, but I can see you editted that now.
@SagiRika Oh damn. I am very sorry for that. I felt a little bit offended. I just copied the question code. Sorry again.
0

Firstly, don't redefine answer (remove var from the loop). Secondly, think about the three possible cases your loop has:

answer = "yes"  ->  (answer != "yes") is false, (answer != "yeah") is true. The loop continues.
answer = "yes"  ->  (answer != "yes") is true, (answer != "yeah") is false. The loop continues.
answer = "anything else"  ->  (answer != "yes") is true, (answer != "yeah") is true. The loop continues.

Using a logical AND && operator should resolve your issue:

var answer = prompt("Are we there yet?");
while (answer != "yes" && answer != "yeah") {
  answer = prompt("Are we there yet?");
}
alert("YAY! We made it!!");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.