Rock, paper, scissors part 9, next steps


#1

hey, im trying to make it so that if the user choosing something that isnt rock, paper, or scissors, an error message comes up reminding them to choose from those three. i really dont know how to do this, but since it is a suggested 'next step' i thought i should be able to work out a way from what i know.

so i kind of made up this extra bit of code out of guesswork and googling how to do it, so it probably makes no sense. ill paste it here and if anyone knows how i should be doing it or if im anywhere near correct then any help would be much appreciated :smile:

after the first bit of code:
var userChoice = prompt("Do you choose rock, paper or scissors?");

i put:
if (userChoice != "rock" || userChoice != "paper" || userChoice != "scissors" ) {
var userChoice = prompt("Please enter rock, paper or scissors!")
}

then the rest of my normal code about var computerChoice etc.

if i put say, banana, into the first prompt box, the second prompt box comes up. if i put rock or paper or scissors in the second box itworks as normal. however, even if i put rock or paper or scissors into the first box the second box always comes up.

i have no idea what im doing. have we been taught something by this point which should give me the answer to this?

thank you, and sorry if im being dense

EDIT: if i change the second prompt box to instead be a console.log message that might work, i just then am not sure if i know how to loop the code, so to speak, so that the original prompt comes up again. then if userChoice isnt one of the acceptable options again, it will be able to print the message and bring up the prompt box an infinite amount of times until the user chooses rock paper or scissors, yes?

EDIT: btw looking back i can definitely see the flaw in the approach ive taken. it appears for all options because userChoice is always not rock or not paper or not scissors,since if you choose rock then you havent chosen scissors or paper, hence the prompt always appears.

so what can i do instead?


#2

Something like this?

var response = ""

while (response doesn't match any option) {
    ask for input
}

or..

while (true) {
    ask for input
    if it matches, break out of the loop
    sorry, invalid option
}

You can definitely modify that condition. Try making use of == instead of !=, because that's much easier to wrap your head around.


#3

try this:

if (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors" )


#4

A post was split to a new topic: The result is always tie


#5

if (userChoice !== "rock" || userChoice !== "paper" || userChoice !== "scissors" ) {
var userChoice = prompt("Please enter rock, paper or scissors!")
}

It means "not equal to " notation varies here.