I have a function which is expected to return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.
getIndexToIns([1,2,3,4], 1.5) should return 1 because 1.5 is greater than 1 (index 0), but less than 2 (index 1).
The code executes as expected except when these argument are passed to it
getIndexToIns([5, 3, 20, 3], 5); //should return 2 but return 0
getIndexToIns([2, 5, 10], 15); //should return 3 but return -1
getIndexToIns([], 1)); //should return 0 but return -1
Here's my code in JavaScript
function getIndexToIns(arr, num) {
arr = arr.sort();
for(let i in arr){
if(num < arr[i]){
arr.splice(i,0,num);
}
}
return arr.indexOf(num);
}