Project rock-paper-scissors-javascript

I finished all the Javascript interactive lessons and am now doing all the projects.

I’ve just finished the Rock, Paper, scissors project and it all works but i wanted to ask about the determinWinner function.

Instead of doing what it said and writing a if statement for ever possible answer i used logical operators with a if…else statement to make it way shorter.

This is my code

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice){
    return 'Draw'
  if ((userChoice === 'rock' && computerChoice === 'scissors') || (userChoice === 'paper' && computerChoice === 'rock') || (userChoice === 'scissors' && computerChoice === 'paper')){
  return 'user wins';
  }else return 'computer wins'

Is it better like this or is doing it only with if statments the way to go and if so why ?


Hello, @danyroyal! Welcome to the Codecademy forum!
That’s actually a good idea as long as it correctly determines the winner, which it appears to do.
Hint: You could shorten it further by having your if statement test for the three computer wins scenarios, and else return ‘user wins’. You wouldn’t need the first if to check for a tie :wink:

Edit: nevermind the hint. I just realized you’re returning ‘draw’ for a tie. I did the project a while back, and was thinking that in case of a tie the user wins. Probably a different project. :man_facepalming:

1 Like