Need finish the quiz with array function

Hello ) can not finish quiz , my function

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

getIndexToIns([5, 3, 20, 3], 5);

can not complite :
getIndexToIns([2, 5, 10], 15)should return 3` .

getIndexToIns([2, 5, 10], 15) should return a number.

getIndexToIns([], 1) should return 0 .

getIndexToIns([], 1) should return a number.

link :https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/where-do-i-belong

It’s an issue with the .sort() function. It sees numbers as a string which results in an incorrect sort. You can see this by console.log(arr). I’m not that familiar with it but I’ll read up on it to see if I can find a solution.
-J

I suggest you read how sort works, because your array is being sorted as [20, 3, 3, 5]

Thank you) now I know how sort() works )

other issues:

  • if arr.length is 0 then the loop is not executed (0 < 0 is false), and your condition to return 0 is never reached
  • you are missing the condition in cui num is bigger than all the numbers in the array

Thanks for help :slightly_smiling_face: )