gpt4 book ai didi

javascript - 重复 我知道 : Rock Paper Scissors Code academy. 为什么没有出现提示?

转载 作者:行者123 更新时间:2023-12-01 04:09:49 24 4
gpt4 key购买 nike

我的下面的提示似乎没有显示。我无法让这段代码工作。我仍然是一名初学者,但我已经编码了大约 3 个月了。

<html>
<title>Random Code</title>

<head>
<style>
body {
background-color: green;
color: white;
}
</style>
<script>
var userChoice = prompt("Choose Rock Paper or Scissor!").toUpperCase();
//this prompt is not showing up.
var computerChoice = Math.random();
if (computerChoice < .34) {
computerChoice = "ROCK";
} else if (computerChoice > .33 && computerChoice <= .67) {
computerChoice = "PAPER";
} else {
computerChoice = "SCISSORS";
}
document.write("Computer: " + computerChoice);

var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return ("The result is a tie!")
} else if (choice1 === "ROCK" && choice2 === "PAPER") {
return ("The computer chose:" + computerChoice + ". You lose");
} else if (choice1 === "ROCK" && choice2 === "SCISSORS") {
return ("The computer chose:" + choice2 + ". You win!");
} else if (choice1 === "PAPER" && choice2 === "ROCK") {
return ("The computer chose:" + choice2 + ". You lose.");
} else if (choice1 === "PAPER" && choice2 === "SCISSORS") {
return ("The computer chose:" + choice2 + ". You lose.");
} else if (choice1 === "SCISSORS" && choice2 === "ROCK") {
return ("The computer chose:" + choice2 + ". You lose.");
} else if (choice1 === "SCISSORS" && choice2 === "PAPER") {
return ("The computer chose:" + computerChoice + ". You win!");
} else {
return ("You entered an invalid input");
}
}

document.write("User choice: " + userChoice);
document.write("Computer choice: " + computer Choice);
document.getElementById("rps").innerHTML = compare(userChoice, computerChoice);
</script>

</head>

<body>
<p id="rps"></p>



</body>

</html>

如果您有任何指示或建议,我将永远感激不已!

最佳答案

线路

document.write("Computer choice: " + computer Choice);

应该是

document.write("Computer choice: " + computerChoice);

您还应该将脚本移动到页面底部。

<html>
<title>Random Code</title>

<head>
<style>
body {
background-color: green;
color: white;
}
</style>


</head>

<body>
<p id="rps"></p>


<script>
var userChoice = prompt("Choose Rock Paper or Scissor!").toUpperCase();
//this prompt is not showing up.
var computerChoice = Math.random();
if (computerChoice < .34) {
computerChoice = "ROCK";
} else if (computerChoice > .33 && computerChoice <= .67) {
computerChoice = "PAPER";
} else {
computerChoice = "SCISSORS";
}

document.write("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return ("The result is a tie!")
} else if (choice1 === "ROCK" && choice2 === "PAPER") {
return ("The computer chose:" + computerChoice + ". You lose");
} else if (choice1 === "ROCK" && choice2 === "SCISSORS") {
return ("The computer chose:" + choice2 + ". You win!");
} else if (choice1 === "PAPER" && choice2 === "ROCK") {
return ("The computer chose:" + choice2 + ". You lose.");
} else if (choice1 === "PAPER" && choice2 === "SCISSORS") {
return ("The computer chose:" + choice2 + ". You lose.");
} else if (choice1 === "SCISSORS" && choice2 === "ROCK") {
return ("The computer chose:" + choice2 + ". You lose.");
} else if (choice1 === "SCISSORS" && choice2 === "PAPER") {
return ("The computer chose:" + computerChoice + ". You win!");
} else {
return ("You entered an invalid input");
}
}

document.write("User choice: " + userChoice);
document.write("Computer choice: " + computerChoice);
document.getElementById("rps").innerHTML = compare(userChoice, computerChoice);
</script>
</body>

</html>

关于javascript - 重复 我知道 : Rock Paper Scissors Code academy. 为什么没有出现提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41580463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com