简介Javascript - 语法

时间:2017-04-21 19:40:09

标签: javascript syntax

我正在尝试使用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:意外的输入结束 我尝试了很多令人尴尬的事情。再次,请原谅我的完全无声。任何帮助将不胜感激.r

3 个答案:

答案 0 :(得分:4)

应关闭onkeyup功能。

<script type="text/javascript">

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

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

</script>

答案 1 :(得分:0)

    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!");
            }
         }

看起来你错过了onkeyup功能的结束括号

答案 2 :(得分:0)

如果你将漏洞脚本复制到浏览器控制台并添加那个缺失的话,你只会错过onKeyUp的clossing。您将看到页面开始显示警报