Rock Paper Scissors Help


#1

I have stared at this code for the better part of two hours and I cannot see where my mistake is. I walked away, came back…still no luck. Can anyone see what the heck is going on here? (Thank you in advance! I am a complete novice when it comes to JS) The syntax error coming up is as follows:
});
^
SyntaxError: Unexpected token )

My code:

const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’){
return userInput;
} else {
console.log(“Error!”)
}
};

const getComputerChoice= () => {
let randomNumber = Math.floor(Math.random() * 3);
switch(randomNumber){
case 0:
return “rock”;
case 1:
return “paper”;
case 2:
return “scissors”;
}
};

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return “The game was a tie!”;
}

if (userChoice === ‘rock’){
if(computerChoice === ‘paper’){
return “The Computer has won”;
} else {
return “The user has won”
}
}

if (userChoice === ‘paper’){
if(computerChoice === ‘scissors’){
return “The Computer has won”;
} else {
return “The user has won”
}

}

if (userChoice === ‘scissors’) {
if(computerChoice === ‘rock’){
return “The Computer has won”;
} else {
return “The user has won”
}
};

const playGame = () => {
const userChoice = getUserChoice(‘scissors’);
const computerChoice = getComputerChoice();
console.log("You threw: "+ userChoice);
console.log(“The computer threw:”+ computerChoice);
console.log(determineWinner(userChoice, computerChoice));
};

playgame();


#2

The user input is to be obtained before running this function. It’s job is to check the input, not get it.


#3

Thank you!! That was it (along with a few other minor mistakes).


#4

I’ve got the same error as hicon2019.

}); ^ SyntaxError: Unexpected token )

But I can’t find that sequence of characters anywhere in my code. I’m sure that I’m overlooking something obvious, but I don’t know what it is.

const getUserChoice = function(userInput) {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') {
     return userInput;
  }
  else {
    console.log('Your choices are rock, paper, or scissors.');
}

const getComputerChoice = function() {
  const randomNumber = Math.floor(Math.random() * 3);
  switch(randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  }
};

const determineWinner = function(userChoice, computerChoice) 
{ if (userChoice === 'bomb')
  	{return 'You destroyed your opponent!';}
  if (userChoice === computerChoice) {
    return 'The game is a tie!';
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';}
    else {
      return 'You won!';
     }
  }
   if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'The computer won!';}
    else {
      return 'You won!';
     }
   }
   if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';}
    else {
      return 'You won!';
     }
  }
};

const playGame = () => {
  const userChoice = getUserChoice('rock');
  const computerChoice = getComputerChoice('');
  console.log("Your choice is: " + userChoice);
  console.log("The computer choice is: " + computerChoice); 
console.log(determineWinner(userChoice, computerChoice));
};
console.log(playGame());

Thank you!


#5

It looks like getUserChoice is missing a closing bracket. :slight_smile: (Or you could say the else in that function is missing a closing bracket.)

To find this kind of stuff, I like to copy my code into Visual Studio Code and press Shift + Alt + F to format it all. For your code, it indents everything after the first console.log, which makes the missing bracket obvious.


#6

Thank you so much! I can’t tell you how long I stared at that!

I’ll try the Visual Studio Code thing, too. Although I think it’s probably shift+option+F for us Mac users :smile: