"Where do I belong" JS exercice

Hi,

My code seems fine, but Free Code Camp tells me it is not good for values

getIndexToIns([2,5,10],15);

My code returns 3 which is correct, but Free Code Camp says it should return the number 3 for these values, which it does!

What is wrong with my code?

Regards,

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

}

getIndexToIns([2,5,10],15);

When I run your code it passes the test for getIndexToIns([2,5,10],15); (returning 3), but it does fail several other tests.

First of all you have to sort the array because not every time sorted array will be passed as parameter.
Secondly, you just need to provide the return statement when the array element is greater than or equals to the element to compare the position at which it would insert. Then after that if the loop executed completely you have to return the arrays length i.e element should be inserted at the end of the array.

Here is the image of your code after editing.

image