提问人:Klynk917 提问时间:11/12/2023 最后编辑:MureinikKlynk917 更新时间:11/12/2023 访问量:31
奥丁项目石头、纸、剪刀游戏
Odin project rock, paper, scissors game
问:
我无法弄清楚为什么游戏不跟踪玩家的分数,它应该在你赢得 5 轮后提醒一条消息,但它没有提醒任何事情,这让我认为它只是在运行时没有跟踪分数该功能。game()
<script>
const rty = Math.floor(Math.random())
let option1 = "rock"
let option2 = "paper"
let option3 = "scissors"
let playerScore = 0
let computerScore = 0
function getComputerChoice() {
if (rty <= 0.33) {
return option1
}
else if (rty <= 0.66) {
return option2
}
else if (rty <= 0.99) {
return option3
}
}
function play(x, enter) {
x = prompt("Enter your choice").toLowerCase()
enter = getComputerChoice()
if (x === "paper" && enter === "rock") {
return alert("You Win! Paper beats Rock");
playerScore++;
}
else if (x === "rock" && enter === "paper") {
return alert("You Lose! Paper beats Rock");
computerScore++;
}
else if (x === "scissors" && enter === "rock") {
return alert("You Lose! Rock beats Scissors");
computerScore++;
}
else if (x === "rock" && enter === "scissors") {
return alert("You Win! Rock beats Scissors");
playerScore++;
}
else if (x === "scissors" && enter === "paper") {
return alert("You Win! Scissors beats Paper");
playerScore++;
}
else if (x === "paper" && enter === "scissors") {
return alert("You Lose! Scissor beats Paper");
computerScore++;
}
else if (x === enter) {
return alert("It's a tie")
}
else {
return alert("Enter a valid input plox")
}
}
function game() {
let win = "Winner"
let lose = "Loser"
for (let i = 0; i < 5; i++) {
play()
if (playerScore === 5) {
return alert(win)
}
}
}
</script>
我将变量放在全局范围内,因此这应该不是问题。
答:
1赞
Mureinik
11/12/2023
#1
您在 ing 之后递增分数,这意味着这些行不会被执行。您应该在返回之前增加分数。例如:return
if (x === "paper" && enter === "rock") {
// First increment the score
playerScore++;
// Then return
return alert("You Win! Paper beats Rock");
}
评论