确认提示无法显示

时间:2017-05-28 16:18:23

标签: javascript html

我必须为一门课程制作一个猜谜游戏。然而,即使我将它与解决方案进行比较,它似乎也很好。但它在浏览器中没有显示任何内容。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Color Guessing Game</title>
  </head>
  <body onload="do_game()">
      <script type="text/javascript">
    var colors = ["blue", "gray", "yellow", "green","black", "cyan", "purple", "silver", "orange", "lime", "magenta", "olive", "maroon", "brown", "darkblue", "lightblue"];
          boolean finished=false:=;
          var target;
          var guess_input;
          var guess;

          function do_game(){
              random=Math.floor(Math.random()*colors.length);
              target=colors[random];

              var message: "I am thinking of one of these colors...\n"
              for (var i = 0 ; i < colors.length ; i++) {
                    message += colors[i];
                    if (i == (colors.length-2)){
                        message += " and finally ";
                    } else if (i != (colors.length-1)) {
                        message += ", ";
                    }
                }

              var guesses=0;
              var finished=false;
              alert(target);

              while(!finished){
                  guess_input=prompt(message).toLowerCase();
                  guess=guess_input;
                  guesses++;
                  finished=check_guess(guess,target);
              }
          }

          function check_guess(guess,target){
                if(guess>colors[target])
                    alert("I don’t recognize that color! Mine is higher alphabetically.")
                    return false;
                if(guess>colors[target])
                    alert("I don’t recognize that color! Mine is lower alphabetically.")
                    return false;
                if(guess==colors[target])
                    //background color, msg, nbr guesses
                    return true;
                else
                    //doesnt exist
                    return false;
            }
      </script>
  </body>
</html>

对我来说,一切似乎都是合乎逻辑的。但是,只要我打开浏览器就没有任何反应。怎么样?

1 个答案:

答案 0 :(得分:1)

boolean finished=false:=;语法无效,应该是:

 var finished = false;

var message: "I am thinking of one of these colors...\n"有冒号而不是等号,应该是:

var message = "I am thinking of one of these colors...\n";

这将使您的代码运行,尽管您仍然需要很多的工作来使逻辑正常工作并确保它在用户输入意外输入时仍然有效。

您应该始终检查开发人员的控制台(按F12并单击&#34; console&#34;选项卡以查看错误消息)。

&#13;
&#13;
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Color Guessing Game</title>
  </head>
  <body onload="do_game()">
  
      <script type="text/javascript">
      
    var colors = ["blue", "gray", "yellow", "green","black", "cyan", "purple", "silver", "orange", "lime", "magenta", "olive", "maroon", "brown", "darkblue", "lightblue"];
          var finished = false;
          var target;
          var guess_input;
          var guess;

          function do_game(){
              random = Math.floor(Math.random()*colors.length);
              target = colors[random];

              // You had a colon instead of an equal sign
              var message = "I am thinking of one of these colors...\n";
              for (var i = 0 ; i < colors.length ; i++) {
                    message += colors[i];
                    if (i == (colors.length-2)){
                        message += " and finally ";
                    } else if (i != (colors.length-1)) {
                        message += ", ";
                    }
                }

              var guesses=0;
              var finished=false;
              alert(target);

              while(!finished){
                  guess_input=prompt(message).toLowerCase();
                  guess=guess_input;
                  guesses++;
                  finished=check_guess(guess,target);
              }
          }

          function check_guess(guess,target){
                if(guess>colors[target])
                    alert("I don’t recognize that color! Mine is higher alphabetically.")
                    return false;
                if(guess>colors[target])
                    alert("I don’t recognize that color! Mine is lower alphabetically.")
                    return false;
                if(guess==colors[target])
                    //background color, msg, nbr guesses
                    return true;
                else
                    //doesnt exist
                    return false;
            }
      </script>
  </body>
</html>
&#13;
&#13;
&#13;