<html>
<head></head>
<body><center><div id="output" style="width:500px; height:500px; background-color:blue;"></div></center></body>
<script>
var printOut = function(text) {
var output = document.getElementById("output");
output.innerHTML = text;
};
var userChoice = prompt("Do you choose Rock, Paper, or Scissors?")
console.log(printOut("You chose " + userChoice + "."));
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "Rock";
} else if (computerChoice <= 0.67) {
computerChoice = "Paper";
} else {
computerChoice = "Scissors";
}
console.log(printOut("The computer chooses " + computerChoice + "."));
var compareChoice = function(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return "The result is a tie!";
}
if (userChoice === "Rock") {
if (computerChoice === "Scissors") {
return "Rock smashes Scissors!";
} else {
return "Paper covers Rock!";
}
}
if (userChoice === "Paper") {
if (computerChoice === "Rock") {
return "Paper covers Rock!";
} else {
return "Scissors cut Paper!"
}
}
if (userChoice === "Scissors") {
if (computerChoice === "Paper") {
return "Scissors cut Paper!";
} else {
return "Rock smashes Scissors!";
}
}
return console.log(printOut(compareChoice(userChoice, computerChoice)));
};
</script>
</html>
当我在浏览器中打开它并输入我的选择时,它仅告诉我计算机的选择,何时应该告诉我,计算机的结果和最终结果。
我认为这与我的标签有关,但我不确定。
最佳答案
实际上,您的选择也被打印在输出区域中。它只是被计算机的选择所取代。尝试将您的printOut
方法修改为如下形式:
var printOut = function(text) {
var output = document.getElementById("output");
output.innerHTML += text;
};
关于javascript - 为什么这只给我计算机的输出?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25395252/