创建一个猜谜游戏,计算机猜测用户输入的数字

Create a guessing game, where the computer guesses the number that the user inputs

鉴于用户输入应介于 1 和 1000 之间,我正在尝试对 1-1000 的(已排序)整数数组进行二分搜索以输出以下内容:

enter image description here

我只想要输出--忽略格式。现在,代码:

const testButton = document.getElementById("test");

testButton.addEventListener('click', () => {
  myArray = new Array(1000);
  for (i = 0; i < 1000; i++) {
    myArray[i] = i + 1;
    //just for visual aid, not to be actual part of final output
    document.getElementById("boop").innerHTML = myArray;
    document.getElementById("boop").innerHTML += `<p>${myArray[myArray.length -1]}</p>`;
  }


  var userInput = parseInt(document.getElementById("input").value);
  let min = myArray[0];
  let max = myArray[myArray.length - 1];

  if (userInput < 1 || userInput > 1000) {
    document.getElementById("boop").innerHTML += "That's not between 1 and 1000.";
  } else {

    while (min < max) {

      userInput = parseInt(document.getElementById("input").value);
      let min = myArray[0];
      let max = myArray[myArray.length - 1];


      let mid = myArray[myArray.length - 1];

      mid = parseInt(Math.floor(mid / 2));
      let count = 0;

      if (userInput == mid) {
        count++;
        document.getElementById("boop").innerHTML += `<p>Guessed ${mid} and got it. Took me ${count} tries.</p>`;
      } else if (userInput > mid) {
        count++;
        document.getElementById("boop").innerHTML += `<p>Guessed ${mid}. Too low.</p>`;
        min = mid + 1;
        mid = parseInt(Math.floor(mid + (mid / 2)));
      } else {
        count++;
        document.getElementById("boop").innerHTML += `<p>Guessed ${mid}. Too high.</p>`;
        max = mid - 1;
        mid = parseInt(Math.floor(mid / 2));
      }
    }

  }


});
<html>

<head>
</head>

<body>
  <input type="text" id="input">
  <button id="test">
Start Game
</button>
  <p id="boop">
  </p>
</body>

</html>

如果你想弄乱我的 JSfiddle,enter link description here

我在 运行 时没有收到任何控制台错误,但它似乎是一个无限循环,因为我的浏览器挂起。我查看了类似的问题,我能找到的最相似的是:enter link description here 但它使用的是 Java,而且我还没有学会 Java(总体上对编程来说还是很新的), 所以有点混乱,但我相信这是一个类似的概念。

提前感谢您提供的任何帮助!

在你的 while 循环中,你每次都重置到起始条件。所以本质上它每次都在不断地猜测 500。

 while (min < max) {
   userInput = parseInt(document.getElementById("input").value);
   let min = myArray[0];
   let max = myArray[myArray.length - 1];

每次 while 循环执行时,最后两行将其重置为初始条件(所以你是对的,它处于无限循环中)。它将它设置为根据猜测更改最大值或最小值,但在下次执行时这些值会被重置(到 1 和 1000)。

我更新了 JSfiddle https://jsfiddle.net/y4ta13es/

无限循环是因为 while 条件永远不会退出,因为条件总是评估为真。

你的大部分代码都是正确的,但有一些小的逻辑缺陷,比如你在 while 循环中定义变量。您应该在外部声明它们,否则这些值将永远不会更新。

这是片段

 while (min < max) {
    index = parseInt((max+min)/2);
      let mid = myArray[index];
      if (userInput == mid) {
        count++;
        document.getElementById("boop").innerHTML += `<p>Guessed ${mid} and got it. Took me ${count} tries.</p>`;
        break;
      } else if (userInput > mid) {
      min = index;
        count++;
        document.getElementById("boop").innerHTML += `<p>Guessed ${mid}. Too low.</p>`;
      } else {
         max = index;
        count++;
        document.getElementById("boop").innerHTML += `<p>Guessed ${mid}. Too high.</p>`;
      }
    }