为什么我的代码每次调用 playRound 函数时都会添加到playerScore,而不是添加到computerScore?
我的项目简介建议在游戏函数中调用 playRound 函数五次,因为我还没有研究如何“循环”代码来重复函数调用。
我的简介:https://www.theodinproject.com/lessons/foundations-rock-paper-scissors
我尝试在调用 playRound 函数时将 1 添加到playerScore 或computerScore(它们被声明为值为0 的全局变量)。
我尝试使用增量运算符 ++ 和 我尝试过使用加法赋值运算符 += 1
我原以为获胜玩家的分数会增加 1。
实际发生了什么: 每次调用 playRound 函数时,playerScore 都会增加 1,这与获胜者的结果不一致。
//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);
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
一些小的更改将解决您的问题。主要的变化是在每一轮中获得玩家选择和计算机选择,而不是只一次。我们还会同时生成 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.as-console-wrapper { max-height: 100% !important; }