Help rock, paper, or scissors. please ( where is the error?)


#1

function getUserChoice(){
var userInput = prompt('Whats is your choice?: rock, paper, or scissors.');
userInput = userInput.toLowerCase();
if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return userInput;
} else {
console.log('error');
}
}

function getComputerChoice(){
var ramdonNumber = Math.floor(Math.random()*3);
switch (ramdonNumber){
case 0:
return 'rock';break;
case 1:
return 'paper';break;
case 2:
return 'scissors';break;
}
}
function determineWinner(userChoice, computerChoice){
if (userChoice === computerChoice){
return 'the game is a tie';
}
if (userChoice === 'rock') {
if (computerChoice === 'scissors');
return 'user won';
} else {
return 'computer won';
}
}

if (userChoice ==='paper'){
if(computerChoice === 'scissors'){
return 'computer won';
} else {
return 'you won';
}
}

if(userChoice === 'scissors'){
if(computerChoice === 'rock')
{ return 'computer won';
} else {
return 'you won';
}
}
function playGame(){
var userChoice = getUserChoice();
var computerChoice = getComputerChoice();
console.log('You choice: ' + userChoice);
console.log('Computer choice: ' +
computerChoice);

console.log(determineWinner(userChoice, computerChoice));
}

playGame();

In the consola : SyntaxError: Illegal return statement


#2

Here is your code properly formatted:

function getUserChoice(){
var userInput = prompt('Whats is your choice?: rock, paper, or scissors.');
userInput = userInput.toLowerCase();
if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return userInput;
} else {
console.log('error');
}
}

function getComputerChoice(){
var ramdonNumber = Math.floor(Math.random()*3);
switch (ramdonNumber){
case 0:
return 'rock';break;
case 1:
return 'paper';break;
case 2: 
return 'scissors';break;
}
}
function determineWinner(userChoice, computerChoice){
  if (userChoice === computerChoice){
    return 'the game is a tie';
  }
  if (userChoice === 'rock') { 
    if (computerChoice === 'scissors') {
      return 'user won';
    } else { 
      return 'computer won';
    }
  } 

  if (userChoice ==='paper'){
    if(computerChoice === 'scissors'){
      return 'computer won';
    } else {
     return 'you won';
    } 
  }

  if(userChoice === 'scissors'){
    if(computerChoice === 'rock'){ 
      return 'computer won';
    } else {
    return 'you won';
    }
  }
}

function playGame(){
var userChoice = getUserChoice();
var computerChoice = getComputerChoice();
console.log('You choice: ' + userChoice);
console.log('Computer choice: ' +
computerChoice);

console.log(determineWinner(userChoice, computerChoice));
}

playGame();

You missed some closing } inside determineWinner function. Try to always use indentation to notice this errors because you will see when some function is not closed properly.


#3

Thank you for the recommendations.