Trouble with Rock, Paper, Scissors

As the title states, i’m having issues with the rock, paper, scissors project in the JavaScript learning path. I can’t quite seem to find the syntax error it’s throwing.

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:50
const userChoice = getUserChoice(‘scissors’);
^
SyntaxError: Identifier ‘userChoice’ has already been declared

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

const getComputerChoice = () => {
  computerChoice = Math.floor(Math.random() * 3);
  if (computerChoice === 0) {
    return 'rock';
  } else if (computerChoice === 1) {
    return 'paper';
  } else if (computerChoice === 2) {
    return 'scissors';
  }
}

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'It was a tie!';
  } 
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'the computer won!';
    } else {
      return 'the user won!';
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'the computer won!';
    } else {
      return 'the user won!';
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'the computer wins!';
    } else {
      return 'the user wins!'
    }
  }
}

const playGame = (userChoice, computerChoice) => {
  const userChoice = getUserChoice('scissors');
  console.log(userChoice);
  const computerChoice = getComputerChoice();
  console.log(computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
};

playGame();

I just don’t see where my error is… I don’t see how it’s already declared and my scope is off…(that’s what I imagine this is all about it scope and misplacing declarations?

Thank you in advance for any help / input.

your parameters:

const playGame = (userChoice, computerChoice) => {

are local variable within the function. You can provide argument for the parameters when you call the function:

playGame(); // arguments are missing

why would you want to overwrite your parameters?

1 Like

I’m sorry, I don’t quite understand what you mean? Shouldn’t the getComputerChoice function provide a random argument? and I thought I had by adding the argument ‘scissors’ in the userChoice variable?

And I don’t quite understand what you mean when your asking

why would you want to overwrite your parameters?

In the playGame function, you have two parameters: userChoice and computerChoice:

const playGame = (userChoice, computerChoice) => {
                        ^^^^^^^^^^^
                        these "variables" have be declared as parameters.

That means, when you write

and

You are telling the compiler “declare constant variables with the same name as the parameters of this function”. This throws the error you are seeing.


What @stetim94 was talking about was the fact you declare the playGame function using the two parameters userChoice and computerChoice, yet when you call the function:

You don’t use any parameters.

I hope this helps!

1 Like

Thank you for the clarification! I still have a few questions though…I thought by setting the declared variables equal to the previously built functions, the functions would run as the set parameters of the argument passed in the playGame function…? (does that make sense or am I just left field lost…?)

btw thank you so much for all the help!!

The other functions (getUserChoice, etc) use the parameters passed into them: userChoice and computerChoice. These you declare in the function, so there is no need to have them declared in the function, and as parameters.

You’re welcome!

1 Like

Thank you so much! You guys cleared it up for me! …just so I am fully sure that I truly understand it though…

Because the userChoice and computerChoice variables are declared and set to call previously built functions as values, it’s unnecessary to call them as parameters in the playGame function. They will be ran and printed because of they are declared/defined inside the playGame function…?

(this may seem minor to you guys but its major for me and truthfully appreciated)

1 Like

Exactly!

1 Like