Create paper scissors rock game. Not getting correct result for 2 userChoice options

Hi all

Tried working on the paper scissors rock game. I’ve tried to do this without using the hints, but I hit a bit of a roadblock. My code for the determineWinner spits out the correct results when userChoice = rock. But when userChoice = scissors or paper, the tie game is correct, but if not a tie, it always spits out that Player won, even if the computer had a winning choice (ie userChoice = paper, computerChoice = scissors). Anyone know why the rock option gives the correct results, but scissors and paper don’t? Hope that makes sense! Thank you in advance :slight_smile: Code is below.

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
if (userInput === 'rock') {
  return userInput
} if (userInput === 'paper') {
  return userInput
} if (userInput === 'scissors') {
  return userInput ;
} else {
  return 'error'
}
  };

//console.log(getUserChoice('rock'));
//console.log(getUserChoice('gun'));

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

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'Game is a tie'
  } if (userChoice === 'rock' && computerChoice === 'paper') {
    return 'Computer won'
       } else {
          return 'Player won'
  } if (userChoice === 'paper' && computerChoice === 'scissors') {
    return 'Computer won'
      } else {
          return 'Player won'
  } if (userChoice === 'scissors' && computerChoice === 'rock') {
    return 'Computer won'
      } else {
          return 'Player won'
      }
  };

console.log(determineWinner('rock', 'rock'));
console.log(determineWinner('rock', 'scissors'));
console.log(determineWinner('rock', 'paper'));

console.log(determineWinner('paper', 'paper'));
console.log(determineWinner('paper', 'rock'));
console.log(determineWinner('paper', 'scissors'));

console.log(determineWinner('scissors', 'scissors'));
console.log(determineWinner('scissors', 'paper'));
console.log(determineWinner('scissors', 'rock'));

You dont seem to be using any “else if” conditionals in your determineWinner(); function.

I believe your code is doing a check for if (UserChoice === computerChoice), then a check for (userChoice === ‘rock’ && computerChoice === ‘paper’), then any other situation its just returning ‘player won’ without checking the rest of the function due to the return.

Try using an else if statement. I believe you can also group together some of your win conditions into a single conditional.

if (userChoice === computerChoice) {
    return 'tie'
} else if ((userChoice === ‘rock’ && computerChoice === ‘paper’) || (userChoice === ‘paper’ && computerChoice === ‘scissors’) || (userChoice === ‘scissors’ && computerChoice === ‘rock’)) {
   return 'computer won'
} else {
   return 'player won'
}

As mentioned in the previous post by @alkurion ,
in the determineWinner,
in your version, there should be only one else (and it should be at the end)
and every if after the first one should be an else if.

The way you have the code now:

  } if (userChoice === 'rock' && computerChoice === 'paper') {
     return 'Computer won'
  } else {
     return 'Player won'

if the user chose “rock” and the computer chose “paper”,
the function returns 'Computer won'
and if the user chose anything else or the computer chose anything else,
then the else stuff runs
so the function returns 'Player won'
… meaning the function ends and no code beyond that in the function runs
(no other cases are checked).

1 Like