Rock, Paper, Scissors Error...help!

I’m hoping someone can help me identify where I went wrong here…I’m getting a syntax error that Identifier userChoice has already been declared, but I can’t figure out where I declared it?

Here’s a link to the forked project: https://www.codecademy.com/workspaces/623cc7da1adbe1a02ac66587

And the code:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
  return userInput;
} else {
  return 'Error!';
}
}

const getComputerChoice = () => {
  const 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 'Tie!';
  }
  if (userChoice === 'rock' && computerChoice === 'paper') {
    return 'Computer won!';
  } else {
    return 'User won!';
  }
  if (userChoice === 'paper' && computerChoice === 'scissors') {
    return 'Computer won!'
  } else {
    return 'User won!'
  }
  if (userChoice === 'scissors' && computerChoice === 'rock') {
    return 'User won!';
  } else {
    return 'Computer won!'
  }
}

const playGame = (userChoice, computerChoice) => {
  const userChoice = getUserChoice('scissors'); 
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(humanChoice, terminalChoice))
};

playGame();

Hello!

In this snippet from your code:

const playGame = (userChoice, computerChoice) => {
  const userChoice = getUserChoice('scissors'); 

You are defining const userChoice, but it already exists as an argument passed into the function. Since redefining (with let/const) an already defined variable is not something you can do, it throws that syntax error.

Since when you call playGame(); you don’t pass in any arguments and those two variables are already defined in the function, do you think it’s needed to have those two parameters at all? Spoiler: No, it’s not and they can be safely removed.

Happy coding! :slight_smile:

1 Like

Thank you so much for responding! Since I’m going to declare userChoice and computerChoice in the following lines then I don’t need to include them as the argument in the function. That makes sense!

2 Likes

Thanks for posting this topic. I was just working on this topic a few days ago and found it difficult. It was informative seeing how you’ve been working on it.