I have an issue with a simple JavaScript program:
var answer = prompt("Are we there yet?");
while(answer != "yes" || answer != "yeah") {
var answer = prompt("Are we there yet?");
}
alert("Yes! We made it!");
The problem is that when I run this on my browser and type either "yes"
or "yeah"
the prompt method keeps appearing unless I change the logical operator from OR (||
) to AND (&&
).
Shouldn't it work with OR (||
)? Aren't I just saying that any of those two answers would be fine and the alert can run?
I have an issue with a simple JavaScript program:
var answer = prompt("Are we there yet?");
while(answer != "yes" || answer != "yeah") {
var answer = prompt("Are we there yet?");
}
alert("Yes! We made it!");
The problem is that when I run this on my browser and type either "yes"
or "yeah"
the prompt method keeps appearing unless I change the logical operator from OR (||
) to AND (&&
).
Shouldn't it work with OR (||
)? Aren't I just saying that any of those two answers would be fine and the alert can run?
- you are executing loop based on answer variable. value in answer variable is either "yes" or "yeah". both at same time not possible. but when you apply AND operator it check for answer value "yes" and "yeah" which is not possible. that's why your loop is not properly working – Krupesh Kotecha Commented May 4, 2016 at 5:22
3 Answers
Reset to default 5Your statement
answer != "yes" || answer != "yeah"
is true if either first or second condition is true. In your case regardless of the text in answer
one of the conditions will always be true. For example:
- if
answer="yes"
then second condition is true - if
answer="yeah"
then first condition is true - if
answer="whatever"
then both conditions are true
What you need to use instead is &&
:
var answer = prompt("Are we there yet?");
while(answer != "yes" && answer != "yeah") {
var answer = prompt("Are we there yet?");
}
alert("Yes! We made it!");
This will be true only when both conditions are met. For example:
- if
answer="yes"
then first condition is false and second condition is true. Overall result is false and you leave the loop. - if
answer="yeah"
then first condition is true and second condition is false. Overall result is false and you leave the loop. - if
answer="whatever"
then both conditions are true and the loop is repeated.
Actually your condition checking is wrong.
here is your code snippet
var answer = prompt("Are we there yet?");
while(answer != "yes" || answer != "yeah") {
var answer = prompt("Are we there yet?");
}
alert("Yes! We made it!");
In the above code snippet, while condition will always true whether it is "yes" or "yeah". you are binding two condition check with (||) OR Operator.
The (||) OR Operation is :-
true || true = true
true || false = true
false || true = true
false || false = false
So, (answar != "yes) || (answar != "yeah") will be true always either you type ("yes","yeah" or some other value).
The while loop will break only if condition return false but in above case it is always true. so it will create infinite loop.
lets try this :-
var answer = prompt("Are we there yet?");
while(answer != "yes" && answer != "yeah") {
var answer = prompt("Are we there yet?");
}
alert("Yes! We made it!");
Due to (&&) AND Operation behavior,This will break infinite loop once any of value ("yes" or "yeah") given in prompt.
(&&) AND Operation behavior :-
true && false = false
false && true = false
false && false = false
true && true = true
So condition will be break once it contains any of value ("yes" or "yeah")
It needs to be an "and" because to get the loop, you need for the answer to be neither "yes" nor "yeah". If the tests were for equality rather than inequality, then you'd use the "or".