If + functions + commas

Hi, i’m working on the rock, paper, scissors exercise and i’m not sure why this code is wrong (i know it’s coming from the if expression) :

console.log('hi');

const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === "rock", "paper", "scissors") {
return userInput;
}
else {console.log("Error");
}
};

console.log(getUserChoice("table")); 

So here the answer is to use || between each argument in the if but i don’t understand why writing the if like i did is wrong.

Consider the following example,

let x = ("a", "b", "c")
console.log(x)
// "c"

// Similarly,
let y = (true, "paper", "scissors") // or (false, "paper", "scissors")
console.log(y) 
// "scissors"

So, your condition simplifies to:

if (userInput === "rock", "paper", "scissors") {
// is the same as:
if ("scissors") {
// which is truthy. Therefore this condition will always be true.

Further reading:

2 Likes