Where do I belong (help. what am I missing)?

Where do I belong (help. what am I missing)?
0

#1

It seems that I am missing something. Can someone help?

Thanks

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  
  
  arr.push(num);
  arr.sort();
  console.log(arr.indexOf(num));
  
  return arr.indexOf(num);
}

#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

Yes, you seem to not be understanding how to correctly use the sort function to sort numbers. You need to supply an applicable compareFunction. Currently, yours does not have such a function, so the function converts the elements to strings and compares them in Unicode code point order. In the test getIndexToIns([3, 10, 5], 3), the 3 gets pushed on the end of the original array [3,10,5] and becomes [3, 10, 5, 3]. Then your sort (with the applicable compare functions) produces [10, 3, 3, 5].


#4

Thank you @ArielLeslie


#5

Thank You @RandellDawson.

Solved:

unction getIndexToIns(arr, num) {
// Find my place in this sorted array.

arr.unshift(num);
arr.sort(function(a,b){
return a - b;
});
console.log(arr.indexOf(num));

return arr.indexOf(num);
}

...