Unexpected code return - Rock paper scissors

I am doing the Rock, Paper, Scissors project. I ran my function to see if it works and it seems my error contingency isn’t working, as it always returns userInput regardless of what it is. I would like to know why.

const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || ‘paper’ || ‘scissors’) {
return userInput
} else {
console.log(‘Error, please type rock, paper, or scissors.’);
}
}

console.log(getUserChoice(‘Paper’)); \logs userInput
console.log(getUserChoice(‘cheese’)); \should log the error but it doesn’t.

I could just copy the video but I would like to know why this isn’t working.

Your if statement needs to follow this pattern…

let  a = "rock" // as an example

if (a === "rock" || a === "paper" || a === "scissors") {
    return a
}

Notice there are three completely independent and complete expressions. It is always the value of, a we are examining in each one.

2 Likes

I see.
I had assumed that since || meant ‘or’ then it could work like a grammatical english sentence and refer back to the subject automatically. I suppose that is just not how javascript works. Thank you very much.

1 Like

That would be correct. We need to be explicit at every turn, and set aside the grammatical nuances which lead to assumptions. Never assume. We write the rules, but the language is based on promises. Our rules can only be carried out on the back of those those promises. It’s why we must study the language, and assume nothing.

1 Like