1

我正在使用 javascript/JQuery 运行这个 html 文档,在回答提示后,什么也没有发生。似乎附加功能不起作用。

这是我的代码:

<!DOCTYPE html>
    <html>
        <head>
            <title>
                Rock Paper Scissors
            </title>
            <script>
                var userChoice = prompt("Do you choose rock, paper, scissors lizard or spock?");
                userChoice = userChoice.toLowerCase();
                var computerChoice = function(){
                    var choices = ["rock", "paper", "scissors", "lizard", "spock"];
                    var random = Math.random();
                    var loop = true;
                    while (loop){
                        i = 0.2;
                        j = 0;
                        if (random <= i){
                            var choices = choices[j]
                            var loop = false;
                        }
                        else {
                            i = i +0.2;
                            j ++;
                        }
                    }
                    return choices;
                };
            $(document).ready(function(){$("body").append(userChoice);});
            $(document).ready(function(){$("body").append(computerChoice());});
        </script>
        </head>
    <body>
    </body>
</html>
4

2 回答 2

3

这有几件事不对劲。

  1. 你没有像其他人指出的那样包含 jQuery
  2. 您对两个不同的值使用了两次变量选项
  3. 你的循环全错了,j 每次都重置为 0,所以你总是要选择“rock”
  4. 你实际上并不需要一个循环,你只需要选择一个数组的随机成员

我已经为你清理了一下-这里的jsfiddle http://jsfiddle.net/7DWhL/

<!DOCTYPE html>
<html>
<head>
<title>Rock Paper Scissors</title>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
var userChoice = "",
    computerChoice = "",
    choices = ["rock", "paper", "scissors", "lizard", "spock"];


$(document).ready( function(){
  userChoice = prompt( "Do you choose rock, paper, scissors lizard or spock?" );
  userChoice = userChoice.toLowerCase();
  computerChoice = choices[ Math.floor( choices.length * Math.random() ) ];

  $( "body" ).append( "<p>" + userChoice + "</p>" );
  $( "body" ).append( "<p>" + computerChoice + "</p>" );
});
</script>
</head>

<body>
</body>
</html>
于 2013-11-02T22:02:19.727 回答
1

主要问题是你得到一个无限循环,因为你在循环内设置了 i = 0.2。

试试这个(小提琴:http: //jsfiddle.net/5c25t/2/):

 $(document).ready(function(){

                var userChoice = prompt("Do you choose rock, paper, scissors lizard or spock?");
                userChoice = userChoice.toLowerCase();
                var computerChoice = function(){
                    var choices = ["rock", "paper", "scissors", "lizard", "spock"];
                    var random = Math.random();
                    var loop = true;
                    var i = 0.2;
                    var j = 0;
                    while (true){
                        if (random <= i){
                            return choices[j]                            
                        }
                        else {
                            i=i+ 0.2;
                            j++;
                        }
                    }
                    return choices;
                };
           $("body").append(userChoice);
           $("body").append(computerChoice());

});
于 2013-11-02T21:46:36.307 回答