title: Binary array search in JavaScript
body: <p>I can't seem to get this code to work right. If they key is in the array the code should alert True and if it's not in the array it should alert false. I can't seem to get it right. Thanks!</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html lang="en-us"&gt;
  
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;Search&lt;/title&gt;
  &lt;script type="text/javascript"&gt;
      function testFunction() {
          var list = [ 1, 2, 3, 4, 5, 6, 7 ];
          var key = 9;
          var result1 = 0;
          //var result2 = 0;
          result1 = search(list, key);
  
      }
      function search(list, key) {
          var left = 0;
          var right = list.length - 1;
          while (left &lt;= right) {
              var mid = left + ((right - left) &gt;&gt;&gt; 1);
              var cmp = key - list[mid];
              if (cmp &gt; 0 ){
                  left = alert("True");
              } else if (cmp &lt; 0){
                  alert("False");
              }else {
                  return mid;
              }
                  return mid;
              }
          }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;h1&gt;Search:&lt;/h1&gt;
  &lt;button type="button" onclick="testFunction()"&gt;Volume&lt;/button&gt;
  &lt;div id="outputDiv"&gt;&lt;/div&gt;
  
  &lt;/html&gt;
  </code></pre>