Array sorting issue(Where do I Belong)

Tell us what’s happening:
Describe your issue in detail here.

I use the following code and fail the case:
getIndexToIns([5, 3, 20, 3], 5) should return 2 .

if i add (a, b) => a - b) inside the arr.sort(), it passed the test.
But suppose arr.sort() is default ascending order, why i need to add the above compare statement to pass the above situation?

  **Your code so far**

function getIndexToIns(arr, num) {
for(let i=0;i<arr.length;i++){
    return i;
return arr.length;
getIndexToIns([2, 5, 10], 15));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

Challenge: Where do I Belong

Link to the challenge:

Because sort’s default comparer function just converts them to strings, and strings sort differently than numbers. Numbers get their value from how far they are from the rightmost digit, but alphabetizing starts with the right most character.

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