Rock, Paper, or Scissors

May I please draw someone’s attention to this code block? I would like to know which code will specifically make the else statement trigger? I expected to see 'You win!" printed to the console when calling determineWinner the third time. What will make the else statement trigger the message? I don’t suspect I will have to invert any sort of if statements in order for my else statement to trigger… Am I missing something here? Thanks in advance!

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice){
    return 'The game is a tie'; 
  if (userChoice === 'rock') {
    if (computerChoice === 'paper'){
      return 'The computer has won!'
    } else {
      return 'You won!'; 


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

this will trigger the else:

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

because then if (userChoice === 'rock') evaluates to true while if (computerChoice === 'paper') will evaluate to false, resulting the else clause to be executed

Your code implementation is incomplete at the moment, you only handle ties and when the user choice rock. The other user choices still need to be implemented

I think I would like to address this question, would you please help me clarify? From my understanding, the else statement in determineWinner should execute if userChoice === ‘paper’ and computerChoice === ‘rock’: This is the logic statement the else statement ought to implement, right? the opposite of the very second comparison. The second comparison is not the tie game, it is the comparison of ‘rock’ vs ‘scissors’. Is this assumption fair?

I think to answer your question, you should look at the overall design pattern of this program. Which consist of 4 parts:

part 1: the inputs are the same (tie)
part 2: the userChoice is rock
part 3: the userChoice is paper
part 4: the userChoice is scissors

so far you own implemented part 1 and 2

For each of the user choices/parts (with the exception of part 1), you have an if/else for the computer choice to determine who won

1 Like

Thanks! I am going to continue working on the challenge, and actually finish it . I think I would like to see this logic implemented with ternary operators. I think I will challenge myself to completing this challenge using ternary operators… I tried the first comparison to determine whether the game was a tie game or not. In terms of using ternary operators, when declaring the return statement: Can I get your ideas on how to complete this block, please?

const determineWinner = (userChoice, computerChoice) => {
  userChoice === computerChoice ? 'The game is a tie' : THIS IS WHERE MY QUESTION LAYS... 

When using ternary operators, we would need to declare what to return if the first expression is falsy. What might we add in the second part of the ternary operator - where I added THIS IS WHERE MY QUESTION LAYS.

Then you need something far more complex, I fiddled with a concept like this, here is what I did:

const choices = ['rock', 'paper', 'scissors'];

const determineWinner = (userChoice, computerChoice) => {
  return userChoice + 1 % 3 == computerChoice ? 'computer wins' : 'you win'

console.log(determineWinner(1, 2));
console.log(determineWinner(0, 2));
console.log(determineWinner(2, 0));

I ignore the tie case, but this what your else would look like then

of course you could also work with the strings and get the indexes (using indexOf) from the array

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.