skip to Main Content

I’m trying to make a JavaScript Rock Paper Scissors game. In the previous version of the game prompt was used to get playerSelection, but the aim of this version is to make it work with buttons.

When I try to run the game it says in the game() function, there is a referenceError: playerSelection is not defined.

I’ve been racking my brain trying to fix it but only further mess up the code and confuse myself. I would greatly appreciate any assistance!


const options = ["rock", "paper", "scissors"];
const rock = document.querySelector("#rock");
const paper = document.querySelector("#paper");
const scissors = document.querySelector("#scissors");
//get player's selection
function clickListener (event) {
    const playerSelection = event.target.id;
    const computerSelection = getComputerChoice();
    playRound(playerSelection, computerSelection);
}
rock.addEventListener("click", clickListener);
paper.addEventListener("click", clickListener);
scissors.addEventListener("click", clickListener);

function getComputerChoice() {
  const choice = options[Math.floor(Math.random() * options.length)];
  return choice;
}
//check the winner
function checkWinner(playerSelection, computerSelection) {
  if (playerSelection == computerSelection) {
    return "Tie";
  } else if (
    (playerSelection == "rock" && computerSelection == "scissors") ||
    (playerSelection == "scissors" && computerSelection == "paper") ||
    (playerSelection == "paper" && computerSelection == "rock")
  ) {
    return "Player";
  } else {
    return "Computer";
  }
}
// play 1 round 
function playRound(playerSelection, computerSelection) {
  const result = checkWinner(playerSelection, computerSelection);
  if (result == "Tie") {
    return "It's a Tie!";
  } else if (result == "Player") {
    return `You Win! ${playerSelection} beats ${computerSelection}`;
  } else {
    return `You Lose! ${computerSelection} beats ${playerSelection}`;
  }
  
}
//game
function game() {
  let scorePlayer = 0;
  let scoreComputer = 0;
  console.log("Welcome!");
  for (let i = 0; i < 5; i++) {
    const computerSelection = getComputerChoice();
    console.log(playRound(playerSelection, computerSelection));
    if (checkWinner(playerSelection, computerSelection) == "Player") {
      scorePlayer++;
    } else if (checkWinner(playerSelection, computerSelection) == "Computer") {
      scoreComputer++;
    }
  }
  console.log("Game Over");
  if (scorePlayer > scoreComputer) {
    console.log("Player was the winner");
  } else if (scorePlayer < scoreComputer) {
    console.log("Computer was the winner");
  } else {
    console.log("We have a tie!");
  }
}
// Function to play the game one more time
const play = document.querySelector("#play");
play.addEventListener("click", playAgain);

function playAgain() {
  game();
}

game();

2

Answers


  1. ReferenceError is because the playerSelection variable is not defined within the game function. You can pass playerSelection as a parameter to the game function, and you should also return the result of each round from the playRound function. I’ve tried to modified above code, havn’t tested but you can check yourself.

    Code:

    const options = ["rock", "paper", "scissors"];
    const rock = document.querySelector("#rock");
    const paper = document.querySelector("#paper");
    const scissors = document.querySelector("#scissors");
    
    // Initialize playerSelection
    let playerSelection = "";
    
    // Get player's selection
    function clickListener(event) {
        playerSelection = event.target.id;
        const computerSelection = getComputerChoice();
        playRound(playerSelection, computerSelection);
    }
    
    rock.addEventListener("click", clickListener);
    paper.addEventListener("click", clickListener);
    scissors.addEventListener("click", clickListener);
    
    function getComputerChoice() {
        const choice = options[Math.floor(Math.random() * options.length)];
        return choice;
    }
    
    //check the winner
    function checkWinner(playerSelection, computerSelection) {
      if (playerSelection == computerSelection) {
        return "Tie";
      } else if (
        (playerSelection == "rock" && computerSelection == "scissors") ||
        (playerSelection == "scissors" && computerSelection == "paper") ||
        (playerSelection == "paper" && computerSelection == "rock")
      ) {
        return "Player";
      } else {
        return "Computer";
      }
    }
    
    // play 1 round 
    function playRound(playerSelection, computerSelection) {
      const result = checkWinner(playerSelection, computerSelection);
      if (result == "Tie") {
        return "It's a Tie!";
      } else if (result == "Player") {
        return `You Win! ${playerSelection} beats ${computerSelection}`;
      } else {
        return `You Lose! ${computerSelection} beats ${playerSelection}`;
      }
    }
    
    // Game
    function game(playerSelection) {
        let scorePlayer = 0;
        let scoreComputer = 0;
        console.log("Welcome!");
        for (let i = 0; i < 5; i++) {
            const computerSelection = getComputerChoice();
            const result = playRound(playerSelection, computerSelection);
            console.log(result);
            if (result === "You Win!") {
                scorePlayer++;
            } else if (result === "You Lose!") {
                scoreComputer++;
            }
        }
        console.log("Game Over");
        if (scorePlayer > scoreComputer) {
            console.log("Player was the winner");
        } else if (scorePlayer < scoreComputer) {
            console.log("Computer was the winner");
        } else {
            console.log("We have a tie!");
        }
    }
    
    // Function to play the game one more time
    const play = document.querySelector("#play");
    play.addEventListener("click", playAgain);
    
    function playAgain() {
        game(playerSelection);
    }
    
    // Start the game
    game(playerSelection);
    
    
    Login or Signup to reply.
  2. The problem is that you declare playerSelection in local environtment of clickListener function. So this variable can’t be accessed in game function. But you declare computerSelection in game function, so there are no problems with that.

    For more info you can check this book: https://javascript.info/closure

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search