Keep getting syntax error: SyntaxError: Identifier 'userChoice' has already been declared

Hello, I thought I had completed this code correctly but I seem to get an error about a variable ‘userChoice’ already being defined but when I watched the video to see where I went wrong it seems that i have the same code as in the video. Any help would be appreciated. Here is my code:

const getUserChoice = (userInput) => { userInput = userInput.toLowerCase(); if ( userInput === "rock" || userInput === "paper" || userInput === "scissors" ) { return userInput; } else { console.log("You have not inputted rock paper of scissors."); } }; function 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 "The game is a tie!"; } if (userChoice === "rock") { if (computerChoice === "paper") { return "The computer won!"; } else { return "You have won!"; } } if (userChoice === "paper") { if (computerChoice === "scissors") { return "The computer won!"; } else { return "You have won!"; } } if (userChoice === "scissors") { if (computerChoice === "rock") { return "The computer won!"; } else { return "You have won"; } } }; const playGame = () => { const userChoice = getUserChoice("rock"); const userChoice = getComputerChoice(); console.log(userChoice); console.log(computerChoice); console.log(determineWinner(userChoice, computerChoice)); }; playGame();

Hi,

There is a typo in the second line of playGame() :

  const userChoice = getUserChoice("rock");
  const userChoice = getComputerChoice();

You are attempting to re-assign userChoice. But I think you really mean to assign computerChoice.

:slight_smile:

■■■■ yeah now I see it. thanks a lot not sure how I missed it when it was staring at me

1 Like