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

Tell us what’s happening: i want return the index of the a number (num) in the sorted array (arr)

Your code so far

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  arr=arr.sort();
  for(var i=0;i<arr.length;i++){
    if(arr[i]==num){
      return i-1;
    }
    else if(arr[i]>num){
      return i;
    }
  }
  
}

getIndexToIns([40, 60], 50);

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/where-do-i-belong

Your sort is not working as you think it is. Review the documentation for the sort function and read the section concerning sorting numbers.

1 Like