Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript程序不工作_Javascript - Fatal编程技术网

Javascript程序不工作

Javascript程序不工作,javascript,Javascript,我目前正在用Javascript编写一个简单的程序。我不明白为什么程序不能正常工作。用户应该能够猜出一个数字,程序会告诉用户是否需要猜得更高或更低。现在,它甚至没有给我第一个提示。谢谢你的帮助 <html> <head> </head> <body> <script> //Guessing Game function guessingGame() { var userNumbe

我目前正在用Javascript编写一个简单的程序。我不明白为什么程序不能正常工作。用户应该能够猜出一个数字,程序会告诉用户是否需要猜得更高或更低。现在,它甚至没有给我第一个提示。谢谢你的帮助

<html>
  <head>
  </head>
  <body>
    <script>
      //Guessing Game
      function guessingGame() {
        var userNumber = prompt("Pick a number 1 through 1000!", "Type your number here");
        var computerNumber = Math.floor(Math.random() * 1000 + 1);
        var tries = 0;
        var done = false;
        while (done === false) {
          if (userNumber < computerNumber) {
            document.write("Your number was too low.");
            tries = tries + 1;
            userNumber = prompt("Please guess again", "Type your number here");
          } else if (userNumber > computerNumber) {
            document.write("Your number was too high.");
            tries = tries + 1;
            userNumber = prompt("Please guess again", "Type your number here");
          } else {
            document.write("Great Job!!! You guessed the number in " + tries + " tries!" );
            done = true;
          }
        }
      }
      guessingGame();
    </script>
  </body>
</html>

//猜谜游戏
函数猜测游戏(){
var userNumber=prompt(“选择一个数字1到1000!”,“在此处键入您的数字”);
var computerNumber=Math.floor(Math.random()*1000+1);
var=0;
var done=false;
while(完成===false){
if(用户编号<计算机编号){
写下(“你的号码太低了。”);
尝试=尝试+1;
userNumber=prompt(“请再猜一次”,“在此处键入您的号码”);
}else if(用户编号>计算机编号){
写下(“你的数字太高了。”);
尝试=尝试+1;
userNumber=prompt(“请再猜一次”,“在此处键入您的号码”);
}否则{
写下(“干得好!!!你猜到了“+trys+”trys!”中的数字);
完成=正确;
}
}
}
猜游戏();
尝试后,此行缺少
+
。语法错误通常会导致整个JavaScript块被丢弃,因此不会运行任何东西

if (userNumber < computerNumber) {
或者说,允许使用无花括号

<script>
// <![CDATA[

    /* code here, can use '<' freely */

// ]]>
</script>

// 

您忘记了行中的另一个连接
+

document.write("Great Job!!! You guessed the number in " + tries " tries!" );
应该是

document.write("Great Job!!! You guessed the number in " + tries + " tries!" );

修复了

的示例让我们从告诉计算机使用Javascript开始:
您不需要告诉类型部分。除非另有说明,否则HTML5将推断它是javascript。请使用浏览器控制台对其进行调试。@Havvy,浏览器很可能会正确,假设是现代浏览器等。但养成语义标记代码的习惯是很好的,因此它不依赖于浏览器。你无法相信仍有这么多人在使用IE6及以下。HTML5中按惯例工作的东西通常也是按惯例工作但在所有浏览器中都没有说明的东西。包括不需要类型标记。将来,我建议在询问StackOverflow之前使用JSLint。downvote是怎么回事?谢谢您的帮助!哎呀!谢谢你的提示,但不是这样。对不起。。。我在编辑代码时搞砸了。非常感谢您的快速响应!
document.write("Great Job!!! You guessed the number in " + tries " tries!" );
document.write("Great Job!!! You guessed the number in " + tries + " tries!" );