Where do I belong-partial output

Where do I belong-partial output
0.0 0

#1

Tell us what’s happening:

Your code so far


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

getIndexToIns([3, 10, 5],  3);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:

It deals okay with no repetetions but not with repetions why??


#2

You have the right idea. The problem is your sort function. By default, sort converts to strings and sorts alphabetically. So, [5, 3, 20, 3] would be sorted as [20, 3, 3, 5] - not what you want.

From the Mozilla page, here is an example of sorting numbers:

var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
  return a - b;
});
console.log(numbers);

// [1, 2, 3, 4, 5]

Also, the sort function sorts in place so you can just apply it to arr directly:

arr.sort(function(a, b) {
//...

and then just use arr everywhere. Yes, it gets a little confusing. Some methods return their value, some do it in place.


#3

You can use the shorter version of sort.

var numbers = [4, 2, 5, 1, 3];
numbers.sort((a,b) => a - b );

console.log(numbers);