<HR> Enter the number you wish to guess
<BR>
<input type="textbox" id="value_to_find" value="0">
<br/>
<button id="click" onclick="find_value">Start guessing</button>
<!-- <div id="guess"></div> -->
JavaScript
function doGuess() {
// Create the array
var array = Array.apply(null, {
length: 1000
}).map(Number.call, Number); // New array
// Initial low and high guess
var high_index = array.length;
var low_index = 0;
// Get the value entered by the user
var value_to_find = document.getElementById('value_to_find').value;
find_value(array, high_index, low_index, value_to_find) ;
}
function find_value(a, h, l, v) {
// Get both index and guess
var guess_index = Math.floor(l + (h - l) / 2);
var guess = a[guess_index];
document.getElementById('output').innerHTML += "<br/>Guess:" + guess + " At index:" + guess_index;
if (guess == v) {
document.getElementById('output').innerHTML += "<br.Solution found at index " + g ;
return output;
}
// Note the recursive call where I replace high guess with last guess
if (guess > v) {
document.getElementById('output').innerHTML += " too high";
find_value(a, guess_index, l, v);
}
// Note the recursive call where I replace high guess with last guess
if (guess < v) {
document.getElementById('output').innerHTML += " too low";
find_value(a, h, guess_index, v);
}
}
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.