即使在调用函数后,警报和提示也不起作用

时间:2016-07-30 21:20:22

标签: javascript alert prompt

我正在创建一个生成用户必须猜测的随机颜色的游戏。这是第一部分,但是当我尝试检查页面时,只显示onload消息。我关掉了弹出窗口拦截器。我打电话给这个功能但仍然没有。

<!doctype html>
<html>
<head> 
    <title>Guess the Color Assignment 2 Part 1</title>      
</head>     

<body onload="do_game()">
<script type="javascript/text">
    var target_index;
    var guess_input_text= "none";
    var guess_input;
    var finished = false;
    var guess = 1;
    var colors=["blue","yellow","red","green","brown","black"];
    colors= colors.sort();      

    function do_game () {

        var random_number = (Math.random() * (colors.length-0)) + 0;
        var random_number_integer = Math.floor(random_number);
        target_index= random_number_integer;
        var target = String(colors[random_number_integer]);
        alert("" + target);     

        while (!finished) {
          guess_input_text = prompt("I am thinking of one of these colors: \n\n" +
                                colors.join(",") + "\n\n What color am I thinking of?");

          guess_input = colors.indexOf(guess_input_text);
          guess += 1;
          finished = check_guess();
        }           
    }       
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您的游戏有效但您需要调用do_game()功能才能启动游戏。此外,check_guess()函数未定义