gpt4 book ai didi

javascript - Javascript 简介 - 语法

转载 作者:行者123 更新时间:2023-11-28 14:53:55 26 4
gpt4 key购买 nike

我正在尝试用 javascript 构建一个石头剪刀布游戏。这是我的代码:

  <script type="text/javascript">
var options = ["r", "p", "s"];
document.onkeyup = function(event) {
console.log("event=", event);
var userGuess = String.fromCharCode(event.which).toLowerCase();

console.log(userGuess);

var computerGuess = options[Math.floor(Math.random() * options.length)];

console.log(computerGuess);

if ((userGuess === "r") && (computerGuess === "p")) {
alert("Paper smothers rock - You lose.");
} else
if ((userGuess === "r") && (computerGuess === "s")) {
alert("Rock smashes scissors - You win!")
} else
if ((userGuess === "p") && (computerGuess === "s")) {
alert("Scissors cuts paper to pieces - You lose.");
} else if ((userGuess === "p") && (computerGuess === "r")) {
alert("Paper smothers rock - You win");
} else if ((userGuess === "s") && (computerGuess === "r")) {
alert("Rock smashes scissors - You lose.");

} else if ((userGuess === "s") && (computerGuess === "p")) {
alert("Scissors cuts paper to pieces - You win!")
} else if (userGuess === computerGuess) {
alert("You tie!");
}

</script>

在控制台中时,我不断收到此错误:Uncaught SyntaxError: Unexpected end of input我已经尝试了很多事情,这很尴尬。再次请原谅我的笨蛋。任何帮助将不胜感激。

最佳答案

onkeyup 功能应该关闭。

<script type="text/javascript">

var options = ["r", "p", "s"];

document.onkeyup = function(event){
//Your code here
};

</script>

关于javascript - Javascript 简介 - 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43550749/

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