title: How to implement binary search in JavaScript
body: <p><a href="https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/p/challenge-binary-search" rel="nofollow">https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/p/challenge-binary-search</a></p>
  
  <p>I was following the pseudo code to implement algorithm on the link but don't know what's wrong with my code.</p>
  
  <p>Here is my code : </p>
  
  <pre><code>/* Returns either the index of the location in the array,
    or -1 if the array did not contain the targetValue */
  
      var doSearch = function(array, targetValue) {
      var min = 0;
      var max = array.length - 1;
      var guess;
  
      while(min &lt; max) {
          guess = (max + min) / 2;
  
          if (array[guess] === targetValue) {
              return guess;
          }
          else if (array[guess] &lt; targetValue) {
              min = guess + 1;
          }
          else {
              max = guess - 1;
          }
  
      }
  
      return -1;
  };
  
  var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 
          41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
  
  var result = doSearch(primes, 2);
  println("Found prime at index " + result);
  
  //Program.assertEqual(doSearch(primes, 73), 20);
  </code></pre>