0

I'm creating a Rock Paper Scissors game in JavaScript which is played from the browser console. I have two variables named playerWins and computerWins. When I declared them, I set them to false. After each round, one of them should become true depending on the winner (or both should stay false if there's not a winner in that round). However, for some reason they never become true in my code. What do you think is the reason?

Here's my code:

// Return a random object for computer
function computerPlay() {
  const gameElements = ['Rock', 'Paper', 'Scissors'];
  return gameElements[Math.floor((Math.random()* gameElements.length))];
}

// Play a round of game
function playRound(playerSelection, computerSelection, playerWins, computerWins) {
  if (playerSelection === computerSelection) {
    return "No one wins. Move on.";
  } else if (playerSelection === 'Rock' && computerSelection === 'Scissors') {
      playerWins = true;
      return "You win! The computer chose Scissors, and Rock beats Scissors.";
  } else if (playerSelection === 'Scissors' && computerSelection === 'Rock') {
      computerWins = true;
      return "You lose! The computer chose Rock, and Rock beats Scissors";
  } else if (playerSelection === 'Paper' && computerSelection === 'Rock') {
      playerWins = true;
      return "You win! The computer chose Rock, and Paper beats Rock.";
  } else if (playerSelection === 'Rock' && computerSelection === 'Paper') {
      computerWins = true;
      return "You lose! The computer chose Paper, and Paper beats Rock.";
  } else if (playerSelection === 'Scissors' && computerSelection === 'Paper') {
      playerWins = true;
      return "You win! The computer chose Paper, and Scissors beats Paper.";
  } else if (playerSelection === 'Paper' && computerSelection === 'Scissors') {
      computerWins = true;
      return "You lose! The computer chose Scissors, and Scissors beats Paper.";
  }
}

function checkWinnerInEachRound(playerWins, computerWins, playerScore, computerScore) {
  if (playerWins) {
    playerScore++;
  } else if (computerWins) {
    computerScore++;
  }
}

function checkAndAnnounceLastWinner(playerScore, computerScore) {
  if (playerScore > computerScore) {
    console.log("Congratulations! You defeated the computer.");
  } else if (playerScore < computerScore) {
      console.log("You lost the game to the computer. Better luck next time!");
  } else {
      console.log("Tie!");
  }
}

// The main function of the game
function game() {
  let counter = 0;
  let playerScore = 0;
  let computerScore = 0;
  let canceled = false;
  while (counter < 5) {
    const computerSelection = computerPlay();
    const playerSelectionOriginal = prompt('Please enter an object.');
    
    if (playerSelectionOriginal === null) {
      console.log("You canceled.");
      canceled = true;
      break;
    } else {
        const playerSelection = playerSelectionOriginal.toLowerCase().charAt(0).toUpperCase() + playerSelectionOriginal.slice(1);

        if ( !(playerSelection === 'Rock' || playerSelection === 'Scissors' || playerSelection === 'Paper') ) {
          console.log("Please choose a valid object.");
      } else {
          // Set a boolean value for the player and computer in each round
          let playerWins = false;
          let computerWins = false;

          console.log(playRound(playerSelection, computerSelection, playerWins, computerWins));
          console.log(playerWins);
          console.log(computerWins);
  
          // Add 1 to either player's or computer's score in each round
          checkWinnerInEachRound(playerWins, computerWins, playerScore, computerScore);
  
          // Increment the counter
          counter++;
      }
    }
  }

  // Compare the scores of the player and computer, then announce the winner
  if (canceled === false) {
    checkAndAnnounceLastWinner(playerScore, computerScore);
  }
}

game();
Röyal
  • 87
  • 9
  • Reassigning the local variable has no effect outside the function. Either return it and use the return value, or declare the variable outside only. – CertainPerformance Oct 23 '21 at 01:51
  • @CertainPerformance I have already declared the variable outside. What do you exactly mean by that? – Röyal Oct 23 '21 at 05:16
  • No, you didn't. `function playRound(playerSelection, computerSelection, playerWins,` They're local. – CertainPerformance Oct 23 '21 at 05:16
  • @CertainPerformance Even if I declare the variables outisde, I still don't get the desired result. I changed my function to `function playRound(playerSelection, computerSelection)`, and Wins variables both stay `false` even after this change. – Röyal Oct 23 '21 at 05:20

0 Answers0