JSFiddle - React, Tailwind, and code Playground

by leiperte

HTML

<h1>
  Guessing Computer
</h1>
Think of a number between 1 and 1000, <br/> enter it in the box 
<input type="textbox" id="guess" /> 
<input type="button" id="button" value="Guess Entered Number" onclick="GuessNumber()" />
<br /> How many tries did the computer take to guess your number ?
<br />
<div id="output">

</div>

JavaScript

var min = 0;
var max = 1000;
var d = "";

function GuessNumber(max, min, d) {

  while (max >= min) {
    var error = "Enter a number between 1 and 1000.";
    var g = parseInt(document.getElementByID("guess").value);
    var guess = Math.floor((max + min) / 2);
    if (guess == g) {
    	d+= "Your number is " + guess + "!";
    } else if (guess < g) {
      min = guess + 1;
      d += guess + " too low" + "<br />";
    } else {
      max = guess - 1;
      d += guess + " too high" + "<br />";
    }
    return error;
  }
  document.getElementById("output").innerHTML = d;
}

function createArray() {
  var array = [];
  for (var i = 0; i <= 1000; i++) {
    array.push(array);
  }
}