2

When I execute playGame(); I want to show this: You have chosen ${userChoice) and the computer has chosen ${computerChoice}. The winner is ${determineWinner}. Based on my understanding, the userchoice is defined at the top of my code, and the computerChoice is defined in the computerChoice-section. So I don't understand. Thanks for the help.

Instead I just get an error saying: ReferenceError: userChoice is not defined at playGame.

FIXED: This is what I did, not sure how or why it works though:

function playGame(){
  userChoice = "rock";
  computerChoice = getComputerChoice();
  console.log(`The user chose ${userChoice}. The computer chose ${computerChoice}. ${determineWinner(userChoice,computerChoice)}`)
}
playGame();


//We're taking the userInput and converting it to lowercase letters and storing it within userChoice
    function getUserChoice(userInput){
      let userChoice = userInput.toLowerCase();
        if(userChoice === "rock" || userChoice === "paper" || userChoice === "scissors"){return userChoice;}
        else{return "That hand doesn't exist.";}
    }

    //We're making a number and converting it to an eqvivalent string
    function getComputerChoice(){
      let computerChoice = Math.floor(Math.random()*3);
        if(computerChoice === 0){return "rock";}
        else if(computerChoice === 1){return "scissors";}
        else if(computerChoice === 2){return "paper";}
            else{return "Input not valid.";}
    }

    //Determining the winner based upon the input and the computer's choice
    function determineWinner(userChoice, computerChoice){
        //Having a tie
        if (userChoice === computerChoice){return "It's a tie!";}
        //If the user types in scissors
      else if(userChoice === "scissors"){
        if(computerChoice === "rock"){return "Computer wins! Rock kills scissors.";}
        else if(computerChoice ==="paper"){return "User wins! Scissors kill paper.";}
      }
        //If the user types in paper
      else if(userChoice === "paper"){
        if(computerChoice === "rock"){return "User wins! Paper kills rock.";}
        else if(computerChoice === "scissors"){return "Computer wins! Scissors kill paper.";}
      }
        //If the user types in rock
      else if(userChoice === "rock"){
        if(computerChoice === "paper"){return "Computer wins! Paper kills rock.";}
        else if(computerChoice === "scissors"){return "User wins! Rock kills scissors."};
      }
    }

    //Function that embodies the other functions and executes the game.
    function playGame(){
      console.log(`You chose ${userChoice}`);
    }
    playGame();


Luka Momcilovic
  • 159
  • 2
  • 16
  • Well, you don't actually call any of those functions you declare. So only playGame will run. And, even if you did call then, using `let userChoice` will restrict the scope of that variable, so it only exists inside the getUserChoice function. – Vinicius Apr 27 '19 at 22:19
  • I'm just starting out with JavaScript so pardon me, but what do you mean that using "let" restricts my scope. Should I have used const? – Luka Momcilovic Apr 27 '19 at 22:31
  • @LukaMomcilovic take a look at https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var – user2340824 Apr 27 '19 at 22:38

2 Answers2

1

Your variable userChoice is not defined in the context of the function playGame, which is why you're seeing the reference error:

function playGame() {
  console.log(`You chose ${userChoice}`); // this is not defined
}

playGame();
Daniel Cottone
  • 4,257
  • 24
  • 39
0

Variables are scoped to the block in which they are defined (or the function in which they're defined in the case of var). So the userChoice in the following code is only in scope inside of getUserChoice:

function getUserChoice(userInput){
  let userChoice = userInput.toLowerCase();
    if(userChoice === "rock" || userChoice === "paper" || userChoice === "scissors"){return userChoice;}
    else{return "That hand doesn't exist.";}
}

Similarly, the userChoice variable in the argument list of determineWinner is only in scope for determineWinner.

In playGame, there is no variable named userChoice which is in scope, hence the error.

Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98
  • Ahh, so when you declare a variable it isn't accessible to all functions globally in a script file? I fixed it right now, but I'm not entirely sure how I did it or why, here it is: I just pasted it to the bottom of the original post, it's more clean that way. – Luka Momcilovic Apr 27 '19 at 22:27