如何将我的摇滚,纸张,剪刀游戏嵌入按钮?
<!DOCTYPE html>
<html>
<button onclick="rockPaperScissors()">Click me</button>
<script type="text/javascript">
var rockPaperScissors(
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
confirm("The result is a tie!";)
}
else if(choice1 === "rock") {
if (choice2 === "scissors") {
confirm("rock wins";)
}
else {
confirm("paper wins";)
}
}
//second one
else if(choice1 === "paper") {
if (choice2 === "rock") {
confirm("paper wins";)
}
else {
confirm("scissors wins";)
}
}
//third one
else if(choice1 === "scissors") {
if (choice2 === "rock") {
confirm("rock wins";)
}
else {
confirm("scissors wins";)
}
}
};
)
</script>
</html>
答案 0 :(得分:0)
您的问题在这里:
var rockPaperScissors(
您需要rockPaperScissors
作为函数,而不是变量。
尝试使用以下代码使rockPaperScissors
成为一个函数:
function rockPaperScissors() {}
或
var rockPaperScissors = function() {}
然后将相关代码放在大括号内({ }
)。
答案 1 :(得分:0)
您尚未正确声明rockPaperScissors功能。因此,当程序试图调用它时,它不起作用。
尝试
function rockPaperScissors() {
// put your code in here
}
答案 2 :(得分:0)
还有更多的拼写错误...更正后的代码包括脚本var rockPaperScissors = function(){
的第一行,并且确认应始终如confirm("paper wins");
<!DOCTYPE html>
<html>
<button onclick="rockPaperScissors()">Click me</button>
<script type="text/javascript">
var rockPaperScissors = function(){
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
confirm("The result is a tie!");
}
else if(choice1 === "rock") {
if (choice2 === "scissors") {
confirm("rock wins");
}
else {
confirm("paper wins");
}
}
//second one
else if(choice1 === "paper") {
if (choice2 === "rock") {
confirm("paper wins");
}
else {
confirm("scissors wins");
}
}
//third one
else if(choice1 === "scissors") {
if (choice2 === "rock") {
confirm("rock wins");
}
else {
confirm("scissors wins");
}
}
};
};
</script>
</html>