-3

I am trying to create a simple rock, paper, scissors game to be played in the console. I am attempting to create an "invalid" message when a player inputs anything other than "rock," "paper," or "scissors."

Here is the if statement that I have devised:

if (playerSelection !== "rock", "paper", "scissors") {
    console.log("Invalid")

This code displays the console.log message in all cases. I can't figure out what the problem is here.

Here is the whole code:

//create variable for each object
const getComputerChoice = ["rock", "paper", "scissors"]

//select one variable randomly
const computerSelection = Math.floor(Math.random() * 
getComputerChoice.length);

//create prompt
const playerSelection = prompt("Rock, Paper, or 
Scissors?").toLowerCase();


//player selection is rock
if (playerSelection === "rock" && getComputerChoice[computerSelection] 
=== "rock") {
    console.log("It's a tie!")
} else if (playerSelection === "rock" && 
getComputerChoice[computerSelection] === "paper") {
    console.log("You lose! Paper beats rock")
} else if (playerSelection === "rock" && 
getComputerChoice[computerSelection] === "scissors") {
    console.log("You win! Rock beats scissors")
}

//player selection is paper
if (playerSelection === "paper" && getComputerChoice[computerSelection] 
=== "rock") {
    console.log("You win! Paper beats rock")
} else if (playerSelection === "paper" && 
getComputerChoice[computerSelection] === "paper") {
    console.log("It's a tie!")
} else if (playerSelection === "paper" && 
getComputerChoice[computerSelection] === "scissors") {
    console.log("You lose! Scissors beats paper")
}

//player selection is scissors
if (playerSelection === "scissors" && 
getComputerChoice[computerSelection] === "rock") {
    console.log("You lose! Rock beats scissors")
} else if (playerSelection === "scissors" && 
getComputerChoice[computerSelection] === "paper") {
    console.log("You win! Scissors beats paper")
} else if (playerSelection === "scissors" && 
getComputerChoice[computerSelection] === "scissors") {
    console.log("It's a tie!")
}

//player inputs invalid term
if (playerSelection !== "rock", "paper", "scissors") {
    console.log("Invalid")
}
  • Hint: `&&`. `,` is not a valid way to specify additional conditions in your `if` statement – jnpdx Sep 01 '22 at 23:27
  • Does this answer your question? [How to compare if multiple strings are not found in a statement from input variable?](https://stackoverflow.com/questions/59313642/how-to-compare-if-multiple-strings-are-not-found-in-a-statement-from-input-varia) – jabaa Sep 10 '22 at 20:00

1 Answers1

1

Your if condition is incorrect. There are multiple solutions:

let playerSelection = "rock";
if (!["rock", "paper", "scissors"].includes(playerSelection)) {
    console.log("Invalid")
} else {
    console.log("Valid")
}
  • Use NOT (!) and logical OR (||).

let playerSelection = "rock";
const isValidChoice = playerSelection === "rock" || 
                      playerSelection === "paper" || 
                      playerSelection === "scissors";
if (!isValidChoice) {
    console.log("Invalid")
} else {
    console.log("Valid")
}
  • Use a Set. Please note: This is less performant for the three items in the array here, but if you have many items to compare to this will be the fastest option as lookups are performed in O(1) instead of O(n) (as is the case for includes()).

let playerSelection = "rock";
// create set once, then only use for lookups
const possibleChoices = new Set(["rock", "paper", "scissors"]);
if (!possibleChoices.has(playerSelection)) {
  console.log("Invalid")
} else {
  console.log("Valid")
}

The "inversion" according to De Morgan's laws will of course also work.

Mushroomator
  • 6,516
  • 1
  • 10
  • 27
  • Thank you for the response. I've reformatted the code with your answer in mind and it is working significantly better. Thank you for the help. – lambchopthekid Sep 10 '22 at 19:52