Where do I belong (Possible FCC problem)

function getIndexToIns(arr, num) {
  arr.sort();
  var less = 0;
  for (i=0;i<=arr.length-1;i++){
    if (num == arr[i]){
      less = i-1;
    } else if (num > arr[i]){
      less = i+1;
    }
  }
  return less;
}

It works for : getIndexToIns([3, 10, 5], 3)
getIndexToIns([5, 3, 20, 3], 5)
but not for: getIndexToIns([10, 20, 30, 40, 50], 30)
The 3 cases seem similar to me. I don’t understand why it doesn’t work for the third one. Could it be a problem with FCC?

Edit: That’s the only case it doesn’t work.