Where do I belong once again

Where do I belong once again
0

#1

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:


#2

The push function returns a number value which is the length of the array after the item has been added. Just sorry arr instead of trying to assign it to another variable.


#3

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…


#4

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.