Rock, Paper, Scissors Ternary Operator problem

Hello :wave:

I have a problem with the ternary operator in my function on the “Rock, Paper, Scissors” exercice. I don’t get why it’s returning “undefined” when I’m trying to test it in the console :

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'It\'s a draw !';
  } else if (userChoice === 'rock') {
    computerChoice === 'paper' ? 'Computer wins' : 'You win !';
  } else if (userChoice === 'paper') {
    computerChoice === 'scissors' ? 'Computer wins' : 'You win !';
  } else if (userChoice === 'scissors') {
    computerChoice === 'rock' ? 'Computer wins' : 'You win !';
  }
}

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

I guess it’s because we can’t use “return” in a ternary operator, because it’s working just fine if I use the classic if / else structure. But what should I do in a case of a ternary operator ?

Thank you so much for helping me out !

You are correct that it’s because of the lack of return due to how ternary operators work, and as such nothing is being returned. However the fix for this is pretty simple, you can just return the result of the ternary operator like so:

else if (userChoice === 'rock') {
   return computerChoice === 'paper' ? 'Computer wins' : 'You win !'
}
2 Likes

I see ! Pretty simple indeed !
Thank you so much for your answer :slight_smile: