JavaScript函数无法打印

时间:2019-02-18 05:49:08

标签: javascript html random

我是JavaScript的新手,我正在做一个上课的项目。到目前为止,还没有任何问题,直到出现此问题为止,在我输入输入内容时,什么都不会打印。当我输入问题时,根本没有任何变化。这个问题只是应该创建一个随机数,然后查看用户是否正确猜到了。

<script>
function playGame() {
    var answer;
    var x = Math.floor((Math.random() * 100) + 1);
    var name = document.getElementById('name').value;
    var guess = document.getElementById('guess').value;
    if (x > guess) {
    var answer = "Guess is too low!");
    }
    else if (x < guess) {
    var answer = "Guess is too high!");
    }
    else if (x == guess) {
    var answer = "You guessed correctly!");
    }
    document.getElementById('final').innerHTML = "Answer: " + answer;
</script>

<h2>Random Number Game by </h2>
    <p>Enter Name: 
       <input id="name" type="text">
    </p>
    <p>Enter Guess: <input id="guess" type="number" min="1" max="100" onchange="playGame()"></p>
    <h2 id="final"></h2>

5 个答案:

答案 0 :(得分:1)

代码中存在语法错误。在)语句中删除结尾的右括号if..elsedocument.getElementById('final').innerHTML = "Answer: " + answer;应该在函数内。 var answer;只需要在脚本开头声明一次。

在{{1}之外将x定义为undefined,如果playGame则将x设置为undefined并将x == guess设置为{ {1}}。

x

答案 1 :(得分:1)

您的代码有一些问题:

  • 您需要通过使用大括号undefined结束它来关闭函数
  • <script> let x; function playGame() { var answer; if (!x) { x = Math.floor((Math.random() * 100) + 1) }; var name = document.getElementById('name').value; var guess = document.getElementById('guess').value; if (x > guess) { answer = "Guess is too low!"; } else if (x < guess) { answer = "Guess is too high!"; } else if (x == guess) { answer = "You guessed correctly!"; x = void 0; } document.getElementById('final').innerHTML = "Answer: " + answer; } </script> <h2>Random Number Game by </h2> <p>Enter Name: <input id="name" type="text"> </p> <p>Enter Guess: <input id="guess" type="number" min="1" max="100" onchange="playGame()"></p> <h2 id="final"></h2>声明的末尾删除}
  • 假设您希望游戏只使用一个随机数,而不希望每次猜测时都保持不变,则可以将)移到answer函数之外。这样var x = Math.floor((Math.random() * 100) + 1);只会设置一次,而不是每次输入新的猜测值
  • 虽然不需要,但不必每次都使用playGame()重新初始化x变量。正如您已经声明的那样,您可以通过引用answer
  • 重新定义它

请参见下面的工作示例:

var

答案 2 :(得分:0)

以下内容对我来说似乎是错误的-

if (x > guess) {
    var answer = "Guess is too low!");
}
else if (x < guess) {
    var answer = "Guess is too high!");
}
else if (x == guess) {
    var answer = "You guessed correctly!");
}

为什么在每个语句的末尾有额外的)?您能否尝试在第一个answer之外定义变量if,然后为其赋值?

答案 3 :(得分:0)

  • 从代码中纠正了一些错字。
  • innerHTML语句应位于函数内部。
  • 使用oninput事件代替onchange,因为当从任何源输入任何输入时,它将触发处理程序功能。

function playGame() {
  var answer;
  var x = Math.floor((Math.random() * 100) + 1);
  var name = document.getElementById('name').value;
  var guess = document.getElementById('guess').value;
  if (x > guess) {
    var answer = "Guess is too low!";
  } else if (x < guess) {
    var answer = "Guess is too high!";
  } else if (x == guess) {
    var answer = "You guessed correctly!";
  }
  document.getElementById('final').innerHTML = "Answer: " + answer;
}
<h2>Random Number Game by </h2>
<p>Enter Name:
  <input id="name" type="text">
</p>
<p>Enter Guess: <input id="guess" type="number" min="1" max="100" oninput="playGame()"></p>
<h2 id="final"></h2>

答案 4 :(得分:0)

它不起作用的原因是因为您没有该函数的右括号,并且在定义answer的每一行上都有多余的括号:

function playGame() {
    var answer;
    var x = Math.floor((Math.random() * 100) + 1);
    var name = document.getElementById('name').value;
    var guess = document.getElementById('guess').value;
    if (x > guess) {
    var answer = "Guess is too low!";
    }
    else if (x < guess) {
    var answer = "Guess is too high!";
    }
    else if (x == guess) {
    var answer = "You guessed correctly!";
    }
    document.getElementById('final').innerHTML = "Answer: " + answer;
}
<h2>Random Number Game by </h2>
<p>Enter Name: 
   <input id="name" type="text">
</p>
<p>Enter Guess: <input id="guess" type="number" min="1" max="100" onchange="playGame()"></p>
<h2 id="final"></h2>