I don’t understand why this doesn’t give the expected result.
function getIndexToIns(arr, num) {
let sortedArr = arr.sort();
for (let i = 0; i < arr.length; i++){
if (sortedArr[i] <= num) {
if (num <= sortedArr[i+1]) {
return i;
}
}
}
}
let res = getIndexToIns([10, 20, 30, 40, 60], 50);
console.log(res);