It does not work for getIndexToIns([5, 3, 20, 3], 5)

Tell us what’s happening:
I dont know why but the gives incorrect output for

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

Your code so far


function getIndexToIns(arr, num) {
arr.sort();
console.log(arr);

for(var i=0;i<arr.length;i++)
{
if(num<=arr[i])
{ return i;}
}
return arr.length;
}

console.log(getIndexToIns([60, 40], 50));

Your browser information:

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

Challenge: Where do I Belong

Link to the challenge:

try to call this function in your editor, and see what values are printed by your console.log statements

[ 20, 3, 3, 5 ] // where this is the sorted array or supposed to be the sorted one
0// and this is the index which was supposed to be the entry point of num
these are the outputs I get, although I do not know why

yes, that is the sorted array you get, so something is not working as you want.
The sort method appears in the curriculum for the first time later, but if you want to use it, you need to learn how to use it.
Otherwise it would be best for you to use a different approach.

okay … thank you :sweat_smile: :sweat_smile: