Reputation: 23
I was trying to make a text adventure game when I encountered many problems. This was expected since I am fairly new to JavaScript, as well as coding in general. One of the problems I encountered was one where I added some code to prevent the possibility of someone not choosing something. No matter what input I gave the prompt window, it just became unresponsive.
Here's the code that I think is responsible:
var weapon = prompt("(prompt text)");
while (weapon != "axe" || weapon != "bow and arrow" || weapon != "rubber chicken"); {
alert("That's not an option!");
weapon = prompt("(prompt text)");
}
Upvotes: 2
Views: 740
Reputation: 239443
while (weapon != "axe" ... weapon != "rubber chicken");
The main problem is the ;
at the end. It says the while
loop has only one statement and ;
is that statement. It is similar to doing
while (weapon != "axe" ... weapon != "rubber chicken") {
;
}
Just remove that ;
, you should be fine.
Note: Since you want to allow only those three values, the condition should have been with the &&
operator
while (weapon != "axe" && ... && weapon != "rubber chicken") {
...
}
When you use ||
operator, if I input weapon
as rubber chicken
, since it doesn't match axe
the condition will be satisfied and you will be asking the same question again and again. When you use &&
operator, it will be truthy only when the value is not equal to all the three values you are comparing.
Upvotes: 4