JSFiddle - React, Tailwind, and code Playground

by Peyton Hessler

HTML

<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(a, h, l, v)">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);
  }
  
  }