Where do I belong once again

Tell us what’s happening:
How do I sort the numbers in the array, .sort won’t work.
How can i get the algorithm to work? I’ve looked for help online to no avail.
thank!
Just need some help understanding why it won’t work and in what instance the sort method would work.

Your code so far

function getIndexToIns(arr, num) {
  
  var array = arr.push(num);
  
  
  array.sort(function(a, b) { 
             return a - b; 
             });
  
  
  for (var i=0; i<array.length; i++) {
    if (array[i] == num) {
      return i;
    }
  }
}

getIndexToIns([40, 60], 50);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 OPR/50.0.2762.67.

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

so the result of arr.push(num); would be 3 right? But how would i then add num to the array so i can sort them then find its position in the array in numerical order, then output it so i can solve this problem, I am confused and it seems like things i knew about javascript werent what i thought they were…

Yes, the result of the above is 3.

Whether you write:

var array = arr.push(num);

or

arr.push(num);

arr still gets the new num added to the end of it. Instead of sorting array, which is not needed anyway, just sort arr and then iterate over arr in your for loop. My point is just use arr.push(num) and forget about using array anywhere in your solution.

1 Like