Rock, paper, or scissors - variable not defined?

This is regarding the Rock, Paper, or Scissors project.

I tried tweaking many small details, but I’m still getting the “userChoice is not defined” reference error at determineWinner and at playGame.

Printing userChoice and computerChoice to console in the end worked fine, and I’ve read many posts here in the forum but haven’t been able to find the problem.

Would love some help troubleshooting the code. Thank you!

var getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
    console.log ('Sorry, that is an invalid input.');
  }
};

var getComputerChoice = () => {
  var randomNum = Math.floor(Math.random() * 3);
  switch (randomNum) {
    case 0:
      return 'rock';
      break;
    case 1:
      return 'paper';
      break;
    case 2:
      return 'scissors';
      break;
  }
}

const determineWinner = (userChocie, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'The game is 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 won!';
    } else {
      return 'The user won!'
    }
  }
}

var playGame = () => {
  var userChoice = getUserChoice('rock');
  console.log ('The user threw ' + userChoice);
  var computerChoice = getComputerChoice();
  console.log ('The computer threw ' + computerChoice);
  console.log (determineWinner(userChoice, computerChoice));
}

console.log(playGame());

Hello @tiangan6948249273, welcome to the forums! The problem appears to be a typo:

Do you mean userChoice?

Also, you might want to look at making your function declarations with const. This is because currently, you have var, which can be changed. Future developers (or even malicious users, if this were to get into a website) could completely destroy your functions. It doesn’t make much of a difference in this, but it is good to get into the habit of doing it.

You might also want to review this line:

Since this functions already console.logs everything within it, there is not need to console.log it when you call it. This actually brings up undefined after the rest of the output:
Screen Shot 2020-05-18 at 17.28.15
This is because when you console.log the result of nothing, you get undefined. The reason it is undefined is because you are returning nothing it the function.

3 Likes

Oh wow, thanks! This is slightly embarrassing but I’m happy that I know what went wrong.

Appreciate the tip!

1 Like

You’re welcome! It always helps to have a second pair of eyes! Have you checked out the other things I put on to my post? They just help to make your code cleaner.
Happy coding!

2 Likes

Yes, very helpful! I feel lucky that a small typo lead to this much good advice. :wink:

3 Likes