Need help in Javascript (Rock, Paper, Scissors)

Hi, I have been trying to solve this problem for quite some time but I couldn’t find the solution to it.
Whenever I try to run the code it always returns a syntax error.

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

else shouldn’t have a condition stated. change else to else if.

1 Like

Syntax errors like this are often result of a typo or improper use of syntax. As @lekha5347026751 pointed out, you have added a condition after the else statement. In JS you are allowed to have a statement directly after the else statement without using curly braces. Because the statement itself is technically valid the javascript engine continues on and finds another statement on the same line with no semicolon to separate it. Notice that the error message in the console points out the location of the syntax error as being the curly brace itself. Often the actual problem is before this point in the code where improper syntax or a typo would be.

/home/ccuser/script.js:8
     } else (computerChoice === 'scissors') {
                                            ^

SyntaxError: Unexpected token '{'

You have two options, change to an ‘else if’ as previously noted, or remove the condition entirely if all other cases have been handled as they are in this example.

1 Like