Where do I belong algorithm

Where do I belong algorithm
0

#1

hi, guys, I hope you fine.
actually, I already solved this problem.
but as usual, I liked to see the other solutions to expand my knowledge.
and I stuck with the advance one … could you please help me to understand this one.
we should return the lowest index at which a value should be inserted into an array.
but I’m so confused how the indexOf method solved it because I know when the number does not exist in the array the indexOf return -1 …

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

getIndexToIns([10, 20, 30, 40, 50], 35)

#2

In the first line:

arr.push(num);

the number is pushed onto the array, so later when we see:

arr.indexOf(num);

there is no possible way for the index of number not to exist, so the return statement will always return a number >= 0.


#3

thank you so much for helping,
I really don’t know how I couldn’t notice the push method :flushed: