ReferenceError: getComputerchoice is not defined


#1

Hi Everyone, I tried different things, but nothing worked so far.

My code looks like this:

function getUserChoice() {
var userInput = prompt('Rock, Paper or Scissors?');
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else {
console.log('Error!');
userInput = userInput.toLowerCase();
}
}

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

and this is what I get: ReferenceError: getComputerchoice is not defined

What do I miss? Thank you very much in advance!


#2

Seems to work fine for me. You may get that error if you call your function outside the scope that it is in, but if that is all your code then things should be working fine.

One thing to note; you will want to put breaks after each of your cases.

switch (randomNumber) {
case 0:
  return 'rock';
  break;
case 1:
  return 'paper';
  break;
case 2:
  return 'scissors';
  break;
}

I would also recommend doing

prompt('Rock, Paper or Scissors?').toLowerCase();

Just because you can't be sure that someone will input exactly rock, paper, or scissors