How can i use or and break


i am trying to make a script so when when the userChoise isn't equal with rock OR paper OR scissors print "Invalid value" and the break and not run the rest of the commands

if(userChoise != "rock" || userChoice != "paper" || userChoice != "scissors"){
console.log("Invalit value");

i did a research about OR and it showed me that this sign || represents OR



'rock' is different from 'paper'
'soda'is different from'paper'

So your condition will be true for both of those. They should be treated differently though.

NOT is quite difficult to wrap your head around. I suggest doing this instead:

!(matches any of the valid options) // fill in the condition

But you should also convince yourself of why it is that your condition doesn't work the way you want. Try to find values for userChoicethat make it turn true and false respectively (evaluate the expression manually, in your head)

Also, watch out for typos, computers are not forgiving about this.


Also Unless you have that block of code you provided inside a loop(like while and generic for-loop) , you cannot use the break statement outside a loop.

Also with regard to handling validity of user input, You can create an array with all your valid options like this

var valid_options= ["rock", "paper", "scissors"];

Now in your conditional block you can use the Array.prototype.indexOf() method to check if the value of userChoice exists in that array.

if(valid_options.indexOf(userChoice) == -1){
    console.log("Invalid Value");   

Since you are checking on lowercase values you can make it easier and add, the String.prototype.toLowerCse() on the line where you are prompting the user

//convert user input to lower case
var userChoice = prompt("Do you choose rock, paper or scissors?").toLowerCase();

Read more on the Array.prototype.indexOf() and Array.prototype.toLowerCase() and its variations


Can you also tell me if this symbol || work as OR?


Yes this symbol works as or (20 character limit ... :smile: )