Stuck on Rock paper scissors


#1


Hi , i'm stuck on the rock paper scissors (in the more recent java course i think)


I get the error "ReferenceError: userChoice is not defined " after i give the prompt my answer but i can't get why it's not defined


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

function getComputerChoice() {
  var randomNumber = Math.floor(Math.random()*2);
  if(randomNumber === 0) {
    return 'rock';
  } else if(randomNumber === 1) {
    return 'paper';
  } else {
    return 'scissors';
  }
}

function determineWinner(getUserChoice , getComputerChoice) {
  if(userChoice === computerChoice) {
    return 'It\'s a draw';
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'you won';
    } else {
      return 'you lost';
    }
  } else if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'you lost';
    } else {
      return 'you won';
    }
  } else if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'you lost';
    } else {
      return 'you won';
    }
  }   
}


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

playGame ();


#2

Problem is here:

function determineWinner(getUserChoice , getComputerChoice) {
  if(userChoice === computerChoice) {
    return 'It\'s a draw';
  }
  ...

As you can see, function takes two parameters: getUserChoice and getComputerChoice, but in function scope you used userChoice and computerChoice which are not defined.
So, the esier way is to change function parameters to correct names:

getUserChoice  => userChoice 
getComputerChoice => computerChoice

Or you need to use declared parameters in function scope.


#3

Ho i understand , thanks a lot!


#4

hi,
you will become some mistake. i have here my own solution:

var getUserChoice = function() {
    var userChoice = prompt("Please choose rock, paper or scissors");
    userInput = userChoice.toLowerCase();
    if(userInput === "rock" || userInput === "paper" || userInput === "scissors") {
        return userInput;
    } else {
        return "Invalid choice";
    }
}

var getComputerChoice = function() {
    var computerChoice = Math.random();
    console.log(computerChoice);
    if (computerChoice < 0.34) {
    	computerChoice = "rock";
    } else if(computerChoice <= 0.67) {
    	computerChoice = "paper";
    } else {
    	computerChoice = "scissors";
    } 
    
    return computerChoice;
}

var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return("The result is a tie!");
    } else if(choice1 === "rock" && choice2 === "scissors" || choice1 === "scissors" && choice2 === "rock" ){
        return("rock wins");
    } else if(choice1 === "paper" && choice2 === "rock" || choice1 === "rock" && choice2 === "paper"){
        return("paper wins");
    } else if(choice1 === "paper" && choice2 === "scissors" || choice1 === "scissors" && choice2 === "paper"){
        return("scissors");
        
    }
}

var userChoice = getUserChoice();
var computerChoice = getComputerChoice();
console.log("userChoice = " + userChoice + "\ncomputerChoice = " + computerChoice)
console.log(compare(userChoice , computerChoice));

#5

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