title: Formal way of getting closest values in array in Javascript, given a value and a sorted array?
body: <p>If I have an array like this:</p>
  
  <pre><code>var array = [1, 3, 4, 5, 9, 10];
  </code></pre>
  
  <p>And I have a value like this:</p>
  
  <pre><code>var value = 8;
  </code></pre>
  
  <p>I want to get this result:</p>
  
  <pre><code>var result = getClosestValues(array, value); // [5, 9]
  </code></pre>
  
  <p>What's the correct/preferred way to do this in javascript?  It seems like this is probably a formal algorithm somewhere.  Maybe like this:</p>
  
  <pre><code>var getClosestValues = function(array, value) {
      var low, high = 0, value;
      for (var i = 0; i &lt; array.length; i++) {
          if (low &lt;= value &amp;&amp; low &lt; array[i])
              low = array[i];
          if (high == value &amp;&amp; high &lt; array[i])
              high = array[i];
      };
      return [low, high];
  }
  </code></pre>
  
  <p>Thanks!</p>