2

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)");
}
thefourtheye
241k53 gold badges466 silver badges505 bronze badges
asked Jan 11, 2015 at 5:24

1 Answer 1

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

answered Jan 11, 2015 at 5:27
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.