My code doesn’t work but i am confidence in my code ,what is the mistake here?

My Code doesn’t work what I miss:

the challenge is:

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.

For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).

this’s my code:

function getIndexToIns(arr, num) {
arr.push(num)
arr.sort();
return arr.indexOf(num)
}

the errors are:

getIndexToIns([3, 10, 5], 3) should return 0
getIndexToIns([5, 3, 20, 3], 5) should return 2
getIndexToIns([2, 5, 10], 15) should return 3

Link to the challenge:

Copy-pasting the instructions doesn’t really say where you are confused. We can read the instructions.

The default sorting won’t do what you expect.

1 Like

Thanks for your guidance, I found the solution

1 Like