Javascript - Math.floor 不起作用

Javascript - Math.floor does not work

我知道还有更多 "Math.floor not working" 问题,但我没有在那里找到答案,所以我提出了这个问题。

我正在制作一个简单的算法,它通过在范围之间跳转来查找您的号码。因此,例如,range = 100,number = 60。它会转到 50,看它更高,所以范围是 50 - 100。然后它跳到 75,看看它是更大还是更小,依此类推,直到找到正确的数字。

为了让这更清楚一点,我想:"I'll round them downwards by using Math.floor."但由于某种原因它不起作用。

我的代码:

var guessTheNumber = function() {

  var validNumber = false; // Geldig nummer = niet waar
  var userNumberHTML = document.getElementById("userNumberHTML"); // Noem HTML element met ID "userNumberHTML" "userNumberHTML"
  var guessedNumbers = document.getElementById("guessedNumbers"); // noem HTML element met ID "guessedNumbers" "guessedNumbers"
  guessedNumbers.innerHTML = "Guessed number(s): ";
  var minNumber = 0; // Minimum wat mensen kunnen invullen
  var maxNumber = 100; // Maximum wat mensen kunnen invullen
  var userNumber = prompt("Tell me a number between 0 and 100, including those (numbers with decimals will be rounded downwards)."); // Vraag gebruiker welk nummer
  userNumber = Math.floor(userNumber); // Nummer wordt naar beneden afgerond
  var counter = 1;
  var result = 0;
  var q = 1;

  if (userNumber >= minNumber && userNumber <= maxNumber) { // Als het een getal is groter of gelijk aan 1 en kleiner of gelijk aan 100
    userNumberHTML.innerHTML = "Your number: " + userNumber; // Geef "x" in HTML de waarde van "userNumber", namelijk het genoemde getal
  } else { // Anders
    while (validNumber == false) { // Zolang validNumber NIET waar is
      userNumber = prompt("This is not a number between 0 and 100. Try again."); // wordt "userNumber" de waarde van de nieuwe prompt
      if (userNumber >= 1 && userNumber <= 100) { // Als het een getal is groter of gelijk aan 1 en kleiner of gelijk aan 100
        userNumberHTML.innerHTML = "Your number: " + userNumber; // Geef "x" in HTML de waarde van "userNumber", namelijk het zojuist genoemde getal
        validNumber = true; // Zet validNumber op WEL waar
      }
    }
  }

  result = (minNumber + maxNumber) / 2;
  Math.floor(result);
  guessedNumbers.innerHTML += result + ", ";

  while (result != userNumber) {
    if (result < userNumber) {
      Math.floor(result);
      minNumber = result;
    } else {
      Math.floor(result);
      maxNumber = result;
    }
    result = (minNumber + maxNumber) / 2;
    guessedNumbers.innerHTML += result + ", ";
    q++;
  }

  guessedNumbers.innerHTML += "and there you go!";
  alert("The PC guessed your number in " + q + " times!");
  if (confirm("Try again?")) {
    guessedNumbers.innerHTML = "Guessed number(s): ";
    guessTheNumber();
  }
}
<!DOCTYPE html>
<html>

<head>
  <title>JS test</title>
</head>

<body>
  <button onclick="guessTheNumber()" style="height: 100px; width: 200px;">Let the Computer find your number!</button>
  <p id="userNumberHTML">Your number: </p>
  <p id="guessedNumbers">Guessed number(s): </p>
  <p id="TEST"></p>

  <!-- SCRIPTS -->
  <!-- <script type="text/javascript" src="guessTheNumber.js"></script> -->
  <script type="text/javascript" src="test.js"></script>
</body>

</html>

我真的不知道我做错了什么,因为用户输入确实 Math.floor 应该如此!感谢您的帮助:)

提前致谢,
布伦特

Math.floor(result) returns 结果,所以你必须使用:

result = Math.floor(result);

Javascript参数是按值传递的,不是按引用传递的,所以函数不能修改参数变量。您必须分配结果:

result = Math.floor(result);

这是因为你的userNumber是一个字符串。只需使用 parseInt(userNumber) 即可。 您还应该添加某种输入类型控件!


编辑: 正如其他人指出的那样,您还必须将值分配给变量。我刚刚看到第一次使用 Math.floor() 并认为这是唯一的问题。

总而言之,您应该: userNumber = Math.floor(parseInt(userNumber)) 一开始,所以你没有得到 NaN,然后使用 result = Math.floor(result) 得到正确的值。

你写了

Math.floor(result);

但它不会修改结果,因此没有效果
考虑使用

result = Math.floor(result);

改为