我的变量不会附加

时间:2013-11-02 21:42:36

标签: javascript jquery html

我正在使用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>

2 个答案:

答案 0 :(得分:3)

这有点不对劲。

  1. 你没有像其他人指出的那样包含jQuery
  2. 您对两个不同的值使用变量选项两次
  3. 你的循环都错了,j每次都重置为0,所以你是 总是选择“摇滚”
  4. 你实际上并不需要循环,你只需要随机选择 数组成员
  5. 我已经为你清理了一下 - jsfiddle here 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>
    

答案 1 :(得分: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());

});