Keep getting error "userInput already defined"

Hello! I’m currently having trouble with the Rock Paper and Scissors project in the Web Development career path. I keep getting an error from my code where userInput has already been defined, but I watched the video in Get Help and he does it like I do. I’m very confused! :frowning:

Here is my code:

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

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

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'game is tied';
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'Computer Wins!';
    }
    if (computerChoice === 'scissors') {
      return 'You Win!';
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'You Win!';
    }
    if (computerChoice === 'scissors') {
      return 'Computer Wins!';
    }
  }
}

function playGame() {
  let userChoice = getUserChoice();
  console.log('User Choice = ' + userChoice);
  let computerChoice = getComputerChoice();
  console.log('Computer Choice = ' + computerChoice);
}

playGame('Rock')

Hello @alexisrambles. The reason you are getting this error is that the variable userInput is a function parameter. This means that it is already ‘defined’,a and thus you shouldn’t use a keyword such as let, you should just write, as though you were changing any other variable:

function aFunction (x) {
x += 5;//You didn't need the let keyword here, as it is a parameter
}

The same goes for arrow-syntax functions.
I hope this helps!

1 Like

I tried that but…

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

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

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'game is tied';
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'Computer Wins!';
    }
    if (computerChoice === 'scissors') {
      return 'You Win!';
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'You Win!';
    }
    if (computerChoice === 'scissors') {
      return 'Computer Wins!';
    }
  }
}

function playGame() {
  let userChoice = getUserChoice();
  console.log('User Choice = ' + userChoice);
  let computerChoice = getComputerChoice();
  console.log('Computer Choice = ' + computerChoice);
}

playGame('Rock')

I forgot I’d tried the let to see if that would help. Apologies.

This new code is still getting that error.

The new code is still getting userChoice already defined? When I run your current code, I don’t get that error (but I do still get an error), which is caused by not providing the correct parameter when calling the getUserChoice function inside playGame.

After refreshing the page, now I’m getting “Cannot read property ‘toLowerCase’ of undefined”

Yes. This is because when you call the function getUserInput here:

You are not providing any parameters for the function, whereas it expects one. Therefore, there is no data being passed into it, and therefore no data to make lower case.

Some JS trivia

JS, unlike most languages, won’t throw an error when a parameter is expected and not provided, instead, it just uses a default undefined. This can make debugging especially frustrating.

1 Like

OH! That’s AMAZING. Thanks so much! I really did not understand it at all.

1 Like