function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr = arr.sort((a,b) => a - b);
Math.floor(num);
for (var i = 0; i < arr.length; i++) {
if (arr[i] > num) {
var index = arr.indexOf(arr[i]);
return index - 1;
}
}
}
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/68.0.3440.84 Safari/537.36.

For the test case getIndexToIns([10, 20, 30, 40, 50], 35), your function returns 2 instead of 3.

As @br3ntor, that Math.floor(num) does nothing for the solution and can be deleted. Also, not sure what the point of using indexOf is since i will be the same as whatever it returns, unless the number is not found. If the number is not found, you definitely do not want to return index - 1.

Delete the index = arr.indexOf(arr[i]) line and think about how you could use i in the return statement. Also, consider the case where the number being searched for already exists in arr. What index would you return (assuming arr has been sorted)? Then consider what value for an index you should return if the entire arr has been iterated over and the for loop is complete.