Rock, paper or scissors - Unexpected token else stopping function from progressing

Hey everyone,

So I’m currently going through the Rock, paper or scissors project in the Javascript course and trying to write the determineWinner function, I’ve included all outcomes and added to the function as I have been following the steps until I get to Step 11 where it asks to check your function at which point I get an unexpected else error. From what I can see I’ve written the control flow as asked in the steps but I’m not exactly sure where I’m going wrong? For starters I think I may have too many curly braces {} or maybe they’re placed incorrectly? Either that or I think I’m getting a bit mixed up between what should be an else if of an else statement, my code is below thanks in advance. :slight_smile:

const getUserChoice = userInput => { userInput = userInput.toLowerCase(); if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') { return userInput; } else { console.log('Error!'); } }; const getComputerChoice = () => { const randomNumber = (Math.floor(Math.random() * 3)) switch (randomNumber) { case 0: return 'rock'; break; case 1: return 'paper'; break; case 2: return 'scissors'; break; } }; function determineWinner(userChoice, computerChoice) { if (userChoice === computerChoice) { return 'The game is tied!' } else { if (userChoice === 'rock') { if (computerChoice === 'paper') { return 'The computer won!'; } else { return 'You won!'; } else if { (userChoice === 'paper') { if (computerChoice === 'scissors') { return 'The computer won!'; } else { return 'You won!'; } else if { (userChoice === 'scissors') { if (computerChoice === 'rock') { return 'The computer won!'; } else { return 'You won!'; } } } } } } } } console.log(determineWinner('paper', 'scissors'));

The Code editor already marks your syntax error on lines 34 and 40:
If you write else if, you need to define a condition. If you just want that block to be executed if the first doesn’t apply, just write else:

if(condition) {
// your code
} else {
// your code
}

or

if(condition) {
// your code
} else if(condition) {
// your code
}
1 Like

Thanks for the help but I’m still getting an unexpected token else on line 34 when I remove the if?

If you just remove the word if from line 40, you get this:

if(condition) {
// your code
}
else {
// your code
}
else {
// your code
}

So the program doesn’t know which block to execute, if the first block doesn’t apply.
In case you have multiple conditions, take care of the order:

if(condition) {
// your code
}
else if(condition){
// your code
}
else {
// your code
}
  1. if
  2. else if (can be there multiple times)
  3. else

I’m sorry I understand what you’re saying but regardless of what I’m doing on line 34 I keep getting unexpected else tokens, I feel like I am defining the conditions within the statements and I’ve tried editing it based on what you’re telling me but I’m having the same issues.

Then post your current code here, please.

You seem to have } in the wrong place in some cases.

You have

  if (userChoice === computerChoice) {
    return 'The game is tied!'
  } else {
     if (userChoice === 'rock') {
      if (computerChoice === 'paper') {
        return 'The computer won!';
      } else {
        return 'You won!';
        } else if {
          (userChoice === 'paper') {

but that should be

  if (userChoice === computerChoice) {
    return 'The game is tied!'
  } else if (userChoice === 'rock') {
      if (computerChoice === 'paper') {
        return 'The computer won!';
      } else {
        return 'You won!';
      } // <- this } was missing from your code
  } else if (userChoice === 'paper') {

You seem to want to check whether userChioce is the same as computerChoice or whether userChice is 'rock' or is 'paper' or is 'scissors' in the outermost level of the if-statements,
but you keep nesting if-statement blocks inside of other if-statement blocks instead [because you left out some of the } that should end some of those if-statement blocks].

(you only need an outer set of if-else stuff, and an inner set of if-else within each … only 2 levels)

userChoice
computerChoice
winner
computerChoice
 
tie
rock
paper scissors
computer user
paper
scissors rock
computer user
scissors
rock paper
computer user
1 Like

Thanks for clearing that up! Yeah I didn’t realise I could do multiple if statements for each win condition within the function which is why I’ve ended up with this mess of nested if statements. I think I’m going to go back to the lessons for a bit and then return once I’ve got the fundamentals of functions and conditionals. Thank you for your help.