Basic Algorithm Scripting: Where do I Belong

Tell us what’s happening:
could not pass this one
getIndexToIns([5, 3, 20, 3], 5) should return 2

Your code so far

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  let i =0;
  let newArr = arr.sort();
  for (i=0; i<newArr.length; i++)
  if (newArr[i]>=num)
  return i;

return newArr.length;

getIndexToIns([40, 60], 50);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:

.sort treats array elements as strings by default, so it often doesn’t result in an array that’s numerically sorted.

Pass it this function to sort the array numerically:

arr.sort(function(a, b) {
  return a - b;

This will not solve your problem, because it will return i the very first instance it finds a smaller value. You will want to use this instead

 if (num <=arr[i]){...} 

The full code will be:
/Spoiler Alert/

//sort the array 
arr.sort((a,b)=>{return a-b});

//first check for an array with all smaller values or an empty array
if (num>arr[arr.length-1] || arr.length ==0){
return arr.length;
//loop through the array and find the exact index that num is greater than
{for (let i = 0; i<arr.length; i++){
if (num<=arr[i]){
return i;