Guessing Game

by aren_anderson

HTML

<h1>
Guessing Game
</h1>
<h3>
Enter a number between 1 and 1000
</h3>

<p>
Enter your number: 
<input type="textbox" id="tbGuess" value="100"/>
</p>
<input type="button" id="btnGuess" value="Guess!" onclick="guess()"/>

<div id="output">
</div>
<div id="o">

</div>

JavaScript

//Global variable for array
var array = [];

//function that guesses the number in the textbox
function guess(){
	//variable to hold output
	var d = "";
  
  //for loop to fill array from 1-1000
	for(var i = 0; i <= 1000; i++){
    array[i] = i;
  }
  
  //variables for counter, min, max, and textbox guess
  var counter = 1;
	var min = 0;
	var max = 999;
  var num = parseInt(document.getElementById("tbGuess").value);
  
  //if else loop to text that the guess is inside the bounds of 1-1000
  if (num < 1 || num > 1000){
  	alert("Please insert a number between 1 and 1000");
    return;
  }
  else{
  	//while loop that consists of binary search algorithm
  	while (min<=max){
    	var cpuGuess = Math.ceil((min+max)/2);
      if (cpuGuess<num){
      	d += "Guessed " + cpuGuess + " - too low. </br>";
        min = cpuGuess + 1;
      }
      else if (cpuGuess>num){
      	d += "Guessed " + cpuGuess + " - too high. </br>";
        max = cpuGuess - 1;      
      }
      else if (cpuGuess==num){
      	d += "Guessed " + cpuGuess + " - Got It! </br> It took me " + counter + " Tries. </br>";
        document.getElementById("output").innerHTML = d;
        return;
      }
      counter++;
    }   
  } 
}

function addNode(){
	var 
}