# How this solution works what if the current element of the array is equal to num

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

the second one counts how many numbers are smaller than the number to insert, if you confront that with the index, you may find out that, being arrays 0-indexed, it works perfectly

but why it did not work with first one?

1 Like

If you have `[40, 40, 40]` and the number `40`, at what index should you add it?

if I had this array with my first solution it will return the arr.length. but for the second solution it will be on zero index

and what would be the correct number based on challenge requirements?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.