如何获得JavaScript来跟踪我的分数增量?
我正在制作基本的岩石剪刀游戏,并认为我正确地编码了分数增量,但是该游戏将分数保持在每轮0。
我尝试在函数和全球范围内初始化变量。我尝试在变量增量的前面添加返回。我尝试使用或不使用返回分数语句显示在PlayRound()函数末尾。我了解游戏要么没有修改变量和/或继续推迟到初始给定值,因此我无法弄清楚为什么或需要做什么来获取变量以维持增量。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Rock Paper Scissors</title>
</head>
<body>
<script>
const choices = ['Rock', 'Paper', 'Scissors'];
let playerScore = 0;
let compScore = 0;
function computerPlay() {
return choices[Math.floor(Math.random() * choices.length)];
}
function playRound(playerSelection, computerSelection, playerScore, compScore) {
computerSelection = computerPlay();
playerSelection = prompt("Rock, Paper, or Scissors? ");
if (playerSelection === 'Rock' && computerSelection === 'Scissors') {
playerScore += 1;
return "You win! Rock beats Scissors!";
} else if (playerSelection === 'Scissors' && computerSelection === 'Paper') {
playerScore += 1;
return "You win! Scissors beats Paper!";
} else if (playerSelection === 'Paper' && computerSelection === 'Rock') {
playerScore += 1;
return "You win! Paper beats Rock!";
} else if (playerSelection === 'Scissors' && computerSelection === 'Rock') {
compScore += 1;
return "You lose! Rock beats Scissors!";
} else if (playerSelection === 'Paper' && computerSelection === 'Scissors') {
compScore += 1;
return "You lose! Scissors beats Paper!";
} else if (playerSelection === 'Rock' && computerSelection === 'Paper') {
compScore += 1;
return "You lose! Paper beats Rock!";
} else {
playerScore += 1;
compScore += 1;
return "Tie!"
}
return playerScore;
return compScore;
}
function game() {
for (let i = 0; i < 5; i++) {
console.log(playRound());
console.log(`Your score: ${playerScore}`);
console.log(`Computer score: ${compScore}`);
}
winner();
}
function winner() {
if (compScore > playerScore) {
console.log("\nThe computer dominated your ass! Better luck next time!")
} else if (compScore < playerScore) {
console.log("\nWay to crush it! You win!")
} else {
console.log("\nHoly shizzers! It's a tie!")
}
}
</script>
</body>
</html>
I am making a basic Rock Paper Scissors game and thought I was coding the score increments correctly, but the game keeps the scores at 0 each round.
I tried initializing the variables within the function as well as globally. I tried adding return in front of the variable increments. I tried with and without the return score statements shown at the end of the playRound() function. I understand that the game either isn't modifying the variable and/or keeps deferring to the initial given value, I just can't figure out why or what I need to do to get the variables to maintain the increments.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Rock Paper Scissors</title>
</head>
<body>
<script>
const choices = ['Rock', 'Paper', 'Scissors'];
let playerScore = 0;
let compScore = 0;
function computerPlay() {
return choices[Math.floor(Math.random() * choices.length)];
}
function playRound(playerSelection, computerSelection, playerScore, compScore) {
computerSelection = computerPlay();
playerSelection = prompt("Rock, Paper, or Scissors? ");
if (playerSelection === 'Rock' && computerSelection === 'Scissors') {
playerScore += 1;
return "You win! Rock beats Scissors!";
} else if (playerSelection === 'Scissors' && computerSelection === 'Paper') {
playerScore += 1;
return "You win! Scissors beats Paper!";
} else if (playerSelection === 'Paper' && computerSelection === 'Rock') {
playerScore += 1;
return "You win! Paper beats Rock!";
} else if (playerSelection === 'Scissors' && computerSelection === 'Rock') {
compScore += 1;
return "You lose! Rock beats Scissors!";
} else if (playerSelection === 'Paper' && computerSelection === 'Scissors') {
compScore += 1;
return "You lose! Scissors beats Paper!";
} else if (playerSelection === 'Rock' && computerSelection === 'Paper') {
compScore += 1;
return "You lose! Paper beats Rock!";
} else {
playerScore += 1;
compScore += 1;
return "Tie!"
}
return playerScore;
return compScore;
}
function game() {
for (let i = 0; i < 5; i++) {
console.log(playRound());
console.log(`Your score: ${playerScore}`);
console.log(`Computer score: ${compScore}`);
}
winner();
}
function winner() {
if (compScore > playerScore) {
console.log("\nThe computer dominated your ass! Better luck next time!")
} else if (compScore < playerScore) {
console.log("\nWay to crush it! You win!")
} else {
console.log("\nHoly shizzers! It's a tie!")
}
}
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过多种方法来改进代码。我删除了您的
playRound()
函数的参数,删除了该功能末尾的return
语句,并添加了game()
在脚本的结尾使其正常工作。playRound()
中的参数强迫该函数每次使用未定义的本地值。您根本没有检查用户给出的输入是否有效。
也许您可以查看这种替代方法:摇滚,纸,剪刀?
There are many ways in which you can improve your code. I removed the arguments of your
playRound()
function, removed thereturn
statements at the end of that function and added thegame()
call at the end of your script to make it work.The arguments in
playRound()
forced the function to work with undefined local values each time.You are not checking at all, whether the input given by the user is a valid one.
Maybe you can check out this alternative way of doing the game: rock,paper,scissors?
您的游戏现在正在运行,只知道您没有处理三次以外的球员进入某些东西的情况
Your game is working now, just know that you didn't handle situation where player enter something outside the three times
我不知道。
但是我可能会解决您的问题,即停止使用全局变量。
JavaScript执行
返回
语句后,它将退出该过程,因此PlayRound
的第二个返回
永远不会被调用。因此,请尝试一下...
删除您的全局分数变量。
根据计算机获胜,领带或播放器获胜,请更改
PlayRound
以返回-1、0或1。在
game
中,声明您的两个分数变量,并根据循环在循环中递增它们,请playRound
函数返回。将最终分数传递给
获胜者
。这也更有意义,因为得分在游戏范围内。
I don't know.
But I might have an answer to your problems, which is to stop using global variables.
Once JavaScript executes a
return
statement, it exits the procedure, soplayround
's secondreturn
will never be called.So try this...
Remove your global score variables.
Instead of changing the score, change
playround
to return -1, 0, or 1 based on if computer wins, a tie, or player wins.In
game
, declare your two score variables, and increment them within yourfor
loop based on what yourplayround
function returns.Pass the final score to
winner
.This makes more sense as well, because the score is within the scope of a game.