Using Switch for Determining Winner in Rock, Paper, and Scissors

I want to use switch instead of if, else, and else if for my Rock, Paper, and Scissors game in JS. How do I do it?

Here’s my code:

const getUserChoice = userInput => {
  userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
    console.log('Invalid input!');
  };
};

const getComputerChoice = () => {
  let computerChoice = Math.floor(Math.random() * 3);
  switch (computerChoice) {
  case 0:
    return 'rock';
    break;
  case 1:
    return 'paper';
    break;
  case 2:
    return 'scissors';
    break;
  }
}

const determineWinner = (userChoice, computerChoice) => {
  switch (determineWinner) {
    case userChoice === computerChoice:
      return 'Tie!';
      break;
    case userChoice === 'rock' && computerChoice === 'paper':
      return 'Computer wins!';
      break;
    case userChoice === 'paper' && computerChoice === 'rock':
      return 'User wins!';
      break;
    case userChoice === 'paper' && computerChoice === 'scissors':
      return 'Computer wins!';
      break;
    case userChoice === 'scissors' && computerChoice === 'paper':
      return 'User wins!';
      break;
    case userChoice === 'scissors' && computerChoice === 'rock':
      return 'Computer wins!';
      break;
    case userChoice === 'rock' && computerChoice === 'scissors':
      return 'User wins!';
      break;
  }
};

const playGame = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
};

playGame();

The above code does not work. Calling determineWinner() results in undefined.

However, the following code works:

const getUserChoice = userInput => {
  userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
    console.log('Invalid input!');
  };
};

const getComputerChoice = () => {
  let computerChoice = Math.floor(Math.random() * 3);
  switch (computerChoice) {
  case 0:
    return 'rock';
    break;
  case 1:
    return 'paper';
    break;
  case 2:
    return 'scissors';
    break;
  }
}

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'Tie!';
  } else if (userChoice === 'rock' && computerChoice === 'paper') {
    return 'Computer wins!';
  } else if (userChoice === 'paper' && computerChoice === 'rock') {
    return 'User wins!'
  } else if (userChoice === 'paper' && computerChoice === 'scissors') {
    return 'Computer wins!';
  } else if (userChoice === 'scissors' && computerChoice === 'paper') {
    return 'User wins!';
  } else if (userChoice === 'scissors' && computerChoice === 'rock') {
    return 'Computer wins!'
  } else if (userChoice === 'rock' && computerChoice === 'scissors') {
     return 'User wins!';
  }
};

const playGame = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
};

playGame();

Hi @shadrchy
welcome to the community!
The case refers to the value of the switch expression:

let computerChoice = 'rock';
switch (computerChoice) {
  case 'rock':
    return 'whatever you like';
    break;

In this example, the variable’s value is a number, that’s why this example works.

Hello, @mirja_t ! Thank you for replying to my question. However, I was trying to achieve the functionality of if else statement with switch. Is that possible?

Refer to this piece of code:

No, you can’t have conditions like that in a switch case. That’s what if else conditions are for. In a switch case, the case has to be the value of the passed variable.

const determineWinner = (userChoice, computerChoice) => {
  switch (determineWinner) {

And here you pass the value of the function itself to the switch statement. But that is not available in the function. The returned value of determineWinner is available in a function call outside of the function scope.

1 Like

Thank you so much for the explanation.