Heim  >  Fragen und Antworten  >  Hauptteil

Wie kann ich den playerScore-Wert speichern und bei jedem Aufruf der playRound-Funktion akkumulieren?

Warum fügt mein Code jedes Mal, wenn er die playRound-Funktion aufruft, zu „playerScore“ hinzu, anstatt zu „computerScore“?

In meiner Projektbeschreibung wurde vorgeschlagen, die Funktion „playRound“ fünfmal in der Funktion „Spiel“ aufzurufen, da ich nicht untersucht habe, wie man den Code „schleifen“ kann, um die Funktionsaufrufe zu wiederholen.

Mein Profil: https://www.theodinproject.com/lessons/foundations-rock-paper-scissors

Ich habe versucht, beim Aufruf der playRound-Funktion 1 zu playerScore oder computerScore hinzuzufügen (sie werden als globale Variablen mit dem Wert 0 deklariert).

Ich habe versucht, den Inkrementoperator ++ und zu verwenden Ich habe versucht, den Additionszuweisungsoperator += 1

zu verwenden

Ich dachte, die Punktzahl des siegreichen Spielers würde um 1 erhöht.

Was tatsächlich passiert ist: Bei jedem Aufruf der playRound-Funktion wird playerScore um 1 erhöht, was nicht mit dem Gewinner übereinstimmt.

//write a program to play 'rock, paper, scissors' game against the computer

//COMPUTER CHOICE- generate random choice of weapon
let choice = ['rock', 'paper', 'scissors'];
//select random array element from weapon array
function getComputerChoice() {
  computerChoice = choice[(Math.floor(Math.random() * choice.length))];
  return computerChoice;
}


//USER CHOICE- assign user choice from prompt input
function getPlayerChoice() {
  playerChoice = prompt('Choose your weapon', 'rock, paper or scissors?');
  return playerChoice;
}


//assign values to player variables 
const playerSelection = getPlayerChoice().toLowerCase();
const computerSelection = getComputerChoice();


//message to return to player
let youWin = `You win, ${playerSelection} beats ${computerSelection}`;
let youLose = `You lose, ${computerSelection} beats ${playerSelection}`;
let youDraw = `It's a draw!`;
//put message options into an array
let message = [youWin, youLose, youDraw];


//make global player score variables
let playerScore = 0;
let computerScore = 0;


//function to play one round
function playRound() {

  if (playerSelection == computerSelection) {
    return youDraw;
  } else if (playerSelection == 'rock' && computerSelection == 'paper') {
    computerScore = computerScore++;
    return message[1]; //you lose
  } else if (playerSelection == 'rock' && computerSelection == 'scissors') {
    playerScore++;
    return message[0]; //you win
  } else if (playerSelection == 'paper' && computerSelection == 'rock') {
    playerScore++;
    return message[0]; //you win
  } else if (playerSelection == 'paper' && computerSelection == 'scissors') {
    computerScore++;
    return message[1]; //you lose
  } else if (playerSelection == 'scissors' && computerSelection == 'rock') {
    computerScore++;
    return message[1]; //you lose
  } else if (playerSelection == 'scissors' && computerSelection == 'paper') {
    playerScore++;
    return message[0]; //you win
  } else {
    return ('oops! Type rock, paper or scissors!')
  }
}


//function to play five rounds and report player as winner or loser at the end
function game() {

  //check code: what values are assigned to player selections?
  console.log('player ', playerSelection);
  console.log('computer ', computerSelection);

  playRound();
  playRound();
  playRound();
  playRound();
  playRound();

  return playRound();
}

console.log(game());
console.log(computerScore);
console.log(playerScore);

P粉460377540P粉460377540258 Tage vor329

Antworte allen(1)Ich werde antworten

  • P粉949267121

    P粉9492671212024-02-04 10:57:40

    一些小的更改将解决您的问题。主要的变化是在每一轮中获得玩家选择和计算机选择,而不是只一次。我们还会同时生成 youWin、youLose 等消息。

    //write a program to play 'rock, paper, scissors' game against the computer
    
    //COMPUTER CHOICE- generate random choice of weapon
    let choice = ['rock', 'paper', 'scissors'];
    //select random array element from weapon array
    function getComputerChoice() {
      computerChoice = choice[(Math.floor(Math.random() * choice.length))];
      return computerChoice;
    }
    
    //USER CHOICE- assign user choice from prompt input
    function getPlayerChoice() {
      playerChoice = prompt('Choose your weapon', 'rock, paper or scissors?');
      return playerChoice;
    }
    
    //make global player score variables
    let playerScore = 0;
    let computerScore = 0;
    
    //function to play one round
    function playRound() {
    
      //assign values to player variables 
      const playerSelection = getPlayerChoice().toLowerCase();
      const computerSelection = getComputerChoice();
    
      //check code: what values are assigned to player selections?
      console.log('player:', playerSelection);
      console.log('computer:', computerSelection);
      
      //message to return to player
      let youWin = `You win, ${playerSelection} beats ${computerSelection}`;
      let youLose = `You lose, ${computerSelection} beats ${playerSelection}`;
      let youDraw = `It's a draw!`;
    
      if (playerSelection == computerSelection) {
    return youDraw;
      } else if (playerSelection == 'rock' && computerSelection == 'paper') {
    computerScore++;
    return youLose;
      } else if (playerSelection == 'rock' && computerSelection == 'scissors') {
    playerScore++;
    return youWin;
      } else if (playerSelection == 'paper' && computerSelection == 'rock') {
    playerScore++;
    return youWin;
      } else if (playerSelection == 'paper' && computerSelection == 'scissors') {
    computerScore++;
    return youLose;
      } else if (playerSelection == 'scissors' && computerSelection == 'rock') {
    computerScore++;
    return youLose;
      } else if (playerSelection == 'scissors' && computerSelection == 'paper') {      playerScore++;
    return youWin;
      } else {
    return ('oops! Type rock, paper or scissors!')
      }
    }
    
    //function to play five rounds and report player as winner or loser at the end
    
    function game() {
      for(let round = 0; round < 5; round++) {
    console.log(`Round #${round+1}`)
    console.log(playRound())
      }
    }
    
    game()
    console.log('Computer score:', computerScore);
    console.log('Player score:', playerScore);
    .as-console-wrapper { max-height: 100% !important; }

    Antwort
    0
  • StornierenAntwort