ReferenceError: UserChoice is not defined


#1

Hi everyone,

I don't understand where is the error with the UserChoice. Thanks for your help


https://www.codecademy.com/courses/learn-javascript/projects/rock-paper-scissors


ReferenceError: UserChoice is not defined


function getUserchoice (){
  var userInput = prompt('What do you choose ?');
	userInput = userInput.toLowerCase();
	
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
  return userInput;
} else  {
  console.log ('Error');
}
}
function getComputerChoice (){
    Math.floor(Math.random() * 3);
  switch (getComputerChoice){
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 3:
      return 'scissors';
                      }
}

function determineWinner (userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'It\'s a tie';
  }
  if (userChoice === 'rock'){
    if (computerChoice === 'scissors') {
      return 'You won';
    } else {
      return 'The computer won';
    }
  }
  if (userChoice === 'paper'){
    if (computerChoice === 'scissors') {
      return 'The computer won';
    } else {
      return 'You won';
    }
  }
    if (userChoice === 'scissors'){
    if (computerChoice === 'rock') {
      return 'The computer won';
    } else {
      return 'You won';
    }
  }
}

function playGame (){
  var userChoice = getUserChoice ();
  var computerChoice = getComputerChoice();
  console.log('The user choice is ' + UserChoice + 'and the computer choice is ' + computerChoice);
  console.log(determineWinner (userChoice, computerChoice));
}

playGame ();


#2

Hi there.

In your playGame function:

console.log('The user choice is ' + UserChoice + 'and the computer choice is ' + computerChoice);

The U in UserChoice is capitalised meaning that it is, in fact, different from the variable you are trying to reference which is userChoice. Hence the error "UserChoice is not defined". Meaning there is no variable defined in that way.


#3

Thank you... I was looking for this mistake but wasn't able to find it .... I was going crazy.

Now I have still have on mistake on my console :

The computer choice is undefined --> why the result for the computer choice is always "undefined"

Thank you :slight_smile:


#4

I found my mistakes, here is the final code which works perfectly :slight_smile:

function getUserChoice (){
  var userInput = prompt('What do you choose ?');
	userInput = userInput.toLowerCase();
	
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
  return userInput;
} else  {
  console.log ('Error');
}
}
function getComputerChoice (){
   randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
      break;
    case 1:
      return 'paper';
      break;
    case 2:
      return 'scissors';
      break;
                      }
}

function determineWinner (userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'It\'s a tie';
  }
  if (userChoice === 'rock'){
    if (computerChoice === 'scissors') {
      return 'You won';
    } else {
      return 'The computer won';
    }
  }
  if (userChoice === 'paper'){
    if (computerChoice === 'scissors') {
      return 'The computer won';
    } else {
      return 'You won';
    }
  }
    if (userChoice === 'scissors'){
    if (computerChoice === 'rock') {
      return 'The computer won';
    } else {
      return 'You won';
    }
  }
}

function playGame (){
  var userChoice = getUserChoice ();
  var computerChoice = getComputerChoice();
  console.log('The user choice is ' + userChoice + ', and the computer choice is ' + computerChoice);
  console.log(determineWinner (userChoice, computerChoice));
}

playGame ();

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.