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


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:

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