I have tried many ways of solving this challenge and it seems not obvious for me in some cases
I have tried this one and it did not work because it might be the num is equal to the current array element we are looping in so I had to use >=
function getIndexToIns(arr, num) {
arr.sort((a, b) => a - b);
console.log(arr)
for(let i = 0; i < arr.length; i++){
if(arr[i] > num){
console.log(arr[i])
return arr[i]
}
}
return arr.length
}
now with this solution how could return the right index when the array only returns the numbers there index less than num what if it is equal to the num ?
function getIndexToIns(arr, num) {
return arr.filter(numb => num > numb).length
}
getIndexToIns([40, 60], 50);
Challenge: Where do I Belong
Link to the challenge: