title: Get index of number if it was inserted into a sorted array
body: <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function getIndexToIns(arr, num) {
    arr.sort(function(a, b) {
      return a - b;
    });
    
    for (var i = 0; i &lt; arr.length; i++) { // cycles through the array
      if (arr[i] &gt;= num) { // if array value is bigger than num 
        return i; // return index pos of num bigger than value
      }
      else if (arr[i] === undefined) { // if not found 
        arr.push(num); // push to array 
        return arr.indexOf(num); // return index pos of new num &lt;-- should return 3 in this case
      }
    }
  }
  
  console.log(getIndexToIns([2, 5, 10], 15)); // Should return 3</code></pre>
  </div>
  </div>
  </p>
  <p>The mission of this is to sort an array, and return the index value of <code>arg2</code> if it were in the array.</p>
  <p>Example: <code>getIndexToIns([10, 20, 30, 40, 50], 35)</code> should return <code>3</code>.</p>
  <p>What I’m having trouble with, is if <code>arg2</code> is not found in the array, to push it into it and return its index value. I can’t seem to make it work.</p>