If...else statement vs Ternary operator in Rock, Paper of Scissors project

Hello!

I’ve performed project “Rock, Paper of Scissors” project using if…else statements (the link to the project is below).

In this part of code I wanted to use Ternary operator instead of if…else:

This is how I did it:

} else if (userChoice === 'rock') {
    computerChoice === 'paper' ?
    return 'The computer won!' :
    return 'You won!';
    }

However, it didn’t work and I received a message “SyntaxError: Unexpected token return”.
And I don’t really understand why it didn’t work. I cannot find any information that I can’t use Ternary operator here.

Please advise what is wrong here.

Link to the project:
https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript

Hi,
the return keyword should always stand in front of the statement:

return computerChoice === 'paper' ? 'The computer won!' : 'You won!';
1 Like

Thank you! It works now :slight_smile:

1 Like