<html>
<head>
<h2>
Juan Alban Franco <br>
Assignment 3: Computer Guessing Game.
</h2>
<script>
var Holder_Array = new Array;
var Holder_String;
function Guessing_Game(val)
{
Holder_String = '';
document.getElementById('d').innerHTML = ""
for (var i = 0; i<1000;i++)
{
Holder_Array[i] = i+1;
}
//document.getElementById('d').innerHTML = Holder_Array;
Start_Guessing(0,1000,val);
}
function Start_Guessing(low,high,val)
{
// have to put this in here or else using the paseInt function the program will iterate forever.
if(val == 1000)
{
document.getElementById('guesses').innerHTML = "You made that too easy!"+"<br>"+" Computer guessed 1000!";
return;
}
mid = parseInt(high-((high-low)/2));
var low_bound = low;
if (mid> val)
{
Holder_String +="Guessed: " + mid + " and it was too high!" +"<br>";
Start_Guessing(low_bound,mid,val);
}
else if(mid<val)
{
Holder_String += "Computer Guessed: " + mid + " and it was too low!" +"<br>";
Start_Guessing(mid,high,val);
}
else if (mid == val)
{
Holder_String += "Computer successfully guessed : " + mid ;
}
else
{
Holder_String = 'Something went wrong';
}
document.getElementById('guesses').innerHTML = Holder_String;
}
</script>
</head>
<div>
Please enter a number between 1 and 1000 below:
<br>
<input type = textbox id=Guess_txt value = "1000">
<input type = button onclick="Guessing_Game(document.getElementById('Guess_txt').value)" id = Guess_btn value = "Start Guessing!">
</div>
<br>
<div id = d>
</div>
<div id = guesses></div>
</html>
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.