title: find closest index of array in javascript
body: <p>Please look at this array:</p>
  
  <pre><code>(function(){
      function closest (num, arr) {
          var curr = arr[0];
          var diff = Math.abs (num - curr);
          for (var val = 0; val &lt; arr.length; val++) {
              var newdiff = Math.abs (num - arr[val]);
              if (newdiff &lt; diff) {
                  diff = newdiff;
                  curr = arr[val];
              }
          }
          return curr;
      }
      var _array = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362],
          _number = 155;
  
      return closest (_number, _array);
  })()
  </code></pre>
  
  <p><code>Result is 162</code></p>
  
  <hr>
  
  <p>Hello,In this array I want to show the closest index to the result!
  <code>The result will be 4</code></p>