SyntaxError: Illegal return statement

I keep getting a syntax error message for this code, what is wrong with it? Please help

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

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

function determineWinner(userChoice, computerChoice) {
  if (computerChoice === userChoice)
   return 'tie';
}

if (userChoice === 'rock') {
  if (computerChoice === 'paper') {
    return 'The computer won!';
  } else {
    return 'You 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('You threw: ' + userChoice);
    console.log('The computer threw: ' + computerChoice);
    console.log(determineWinner(userChoice, computerChoice));
}

playGame();
1 Like

your error is illegal return statement

what does this mean?

it means your return statemean is out side of function.

no here

function getUserChoice() {
  var userInput = prompt('Rock, Paper, or Scissors?');
  userInput = userInput.toLowerCase();
}  // you closed the function here

} should be before getComputerChoice function

next same error for

function determineWinner(userChoice, computerChoice) {
  if (computerChoice === userChoice)
   return 'tie';
}  // here you did same

next error undefined randomNumber for

switch (randomNumber)

from where you got randomNumber ?

think and try yourself :slight_smile:

2 Likes

hey thanks for the reply, when i repositioned the } to the correct place the prompt window finally showed up! but then i try changing the “randomNumber” to getComputerChoice, but now i am getting this error message TypeError: Cannot read property ‘toLowerCase’ of null

1 Like

this is the new code

function getUserChoice() {
var userInput = prompt(‘Rock, Paper, or Scissors?’);
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 2:
return ‘scissors’;
}
}

function determineWinner(userChoice, computerChoice) {
if (computerChoice === userChoice)
return ‘tie’;

if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’) {
return ‘The computer won!’;
} else {
return ‘You 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('You threw: ’ + userChoice);
console.log('The computer threw: ’ + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}

playGame();

1 Like

nvm! seems that i checked the box that prevents box from opening haha just had to re-open project and now program is running smooth! thanks a bunch!! although, no matter what i enter in prompt box, i cant seem to lose no matter what answer iI give. what can be causing this?

1 Like

nvm again! lol replaced the switch to “Math.floor(Math.random() * 3)” and now everything seems in order. lost a few times, tied once haha

thanks a lot man!

1 Like

switch (getComputerChoice)

what does this mean?
your function name is getComputerChoice .you are using same name for parameter it’s very bad.

dont do that. randomNumber is better. you just need to do

inside your first posted code

function getComputerChoice() {
  Math.floor(Math.random() * 3);
  
  switch (randomNumber) {

you didnt take a parameter . from where randomNumber came? so i need a parameter named randomNumber here
function getComputerChoice(randomNumber)

now add the random value to it

randomNumber= Math.floor(Math.random() * 3);

3 Likes

Hope you’ve understand now.
feel free to ask any question :slight_smile:

2 Likes