//Assignment 3
var d = ""; // Output
var t = 0; // Counter
var min = 0; // Variable for minimum value
var max = 1000; // Variable for maximum value
var g = max/2; // Variable jalf of max
function findNumber() {
var x = parseInt(document.getElementById("number").value);
if (x < 1 || x > max) {
d = "Only numbers between 1 and " + max + " are allowed ... press Run the fiddle again."
displayGuess();
} else {
if (g < x) {
t++;
d += "We guessed " + g + " which is too low!<br/>";
displayGuess();
min = g;
g = Math.round((min + max) / 2);
findNumber();
} else if (g > x) {
t++;
d += "We guessed " + g + " which is too high!<br/>";
displayGuess();
max = g;
g = Math.round((min + max) / 2);
findNumber();
} else if (g == x) {
t++;
d += "<br/>Your number is " + g + ".<br/> It took us " + t + " tries.<br/><br/>" +
"Press Run the fiddle if you want us to guess another number.";
displayGuess();
t = "0";
} else {
d += "Press Run the fiddle and enter a number for us to guess.<br/>";
displayGuess();
}
}
}
function displayGuess() {
document.getElementById("output").innerHTML = d;
}
function hide(target) {
document.getElementById(target).style.display = 'none';
}
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.