First, create the array! -> <input type="button" value="Create Array"
onClick="fillArray();" />
<br/>
Enter a number between 1 and 1000 for the computer to guess:
<br/>
<input type="textbox" value="" id="number">
<br/>
<input type="button" value="Guess my number!" id="guess" onClick="validate();">
<div id="output">
</div>
JavaScript
var array = []
var d = ""
function fillArray(){
for (i = 0; i < 1001; i++) {
array[i]=i+1
}
displayArray();
}
function displayArray(){
for (i = 0; i < array.length - 1; i++) {
d += array[i] + "<br/>"
}
document.getElementById("output").innerHTML = d;
}
function validate(){
var n =
parseInt(document.getElementById("number").value);
if (s > 1000 || s < 1) {
window.alert("Not a valid number");
}
guess();
}
//something in the guess function is killing it
var a = 1000
var b = 0
var c = 0
function guess(){
//Logic Goal:
//while c != n
//var a = 1000 var b = 0 var c = ? d = ?
//var a + var b = var c / 2 guessed 500 = var c if var c = var s then print guessed correctly in i times
//if c > s print guessed to high then set c to a. a + b = c / 2. compare c to n
// if c < s print guessed to low then set c to b. b + a = c / 2 compare c to n
while (c != n) { //loop until c = n
if (c < n) {
var b = var c
var c = Math.ceil(a + b) / 2
i++;
document.getElementbyId("output").innerHTML = "Guessed to low" + var c "</br>";
}
else if (c > n){
var a = var c
var c = Math.ceil(a + b) / 2
i++;
document.getElementbyId("output").innerHTML = "Guessed to high" + var c "</br>";
}
else {
document.getElementbyId("output").innerHTML = var c + "Got it! In" + i +"tries" "</br>"; }
}//exit loop
}
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.