Question about the "Rock, Paper, or Scissors" exercise

My question is about this exercise:
https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript

Why is in step 8 it says to write a separate if statement from the previous “tie” if statement?
I just continued the “tie” statement and added the user options as else if statements:

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'The game was a tie';
  } else if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won';
    } else if (computerChoice === 'scissors') {
      return 'The user won';
    }
  } else if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'The user won';
    } else if (computerChoice === 'scissors') {
      return 'The computer won';
      }

  } else if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won';
    } else if (computerChoice === 'paper') {
      return 'The user won';
      }
  }
}

It works as intended but is there any advantages to writing it according to step 8?

Hello, @celerclicks, and welcome to the forums.

Not really. No advantages to writing it the way you did either. They are essentially the same since the first if block contains a return statement. If the first if block just modified something, but didn’t exit the function with a return, and you didn’t want to test the condition in the following if statement, then the else if would be the way to go.

If you feel like challenging yourself a bit, consider how you could refactor your code a bit to eliminate all of the else if's. When there’s only two possibilities, and you’ve ruled out one, do you need a test to see if the only other possibility exists?

1 Like