How would i forbbid any answer but scissor, paper or rock?


#1

var userChoice = prompt("Do you choose rock, paper or scissors?");
if(userChoice !== "rock", "paper", "scissors"){
    prompt("You need to choose rock, paper or scissors?");
}

I've tried this way but it's not working. What i want to add to the code is that if someone answers on prompt anything else than scissor, paper or rock it asks him again and again untill he answers correctly.


#2

ideally you would use a loop for this. But loops aren't covered yet. You can also achieve it with function, but its far more complicated

If you want to check multiply conditions, you need to logical operators (which also aren't covered)

Do i need to provide a solution? Or do you think its wiser to progress with the course first, learning the things i just told you?


#3

Oh I see, I thought i should be able to do it myself with current knowledge.
Thank you i will just proceed then and will find it out myself in the next lessons!


#4

you can, using functions.


#5

Could i get this solution? I would like to analyse it because my logic with

if(userChoice !== "rock", "paper", "scissors"){
    prompt("You need to choose rock, paper or scissors?");
}

is obviously not good


#6

the function solution? Yea, let me code it real quickly

something like this:

var validate = function(){
    var userChoice = prompt("Do you choose rock, paper or scissors?");
    if (userChoice == "rock"){
        return "rock"
    }
    else if (userChoice == "paper"){
        return "paper"
    }
    else if (userChoice == "scissors"){
        return "rock"
    } else {
        return validate()
    }
}
userChoice = validate()

#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.