Where do I belong - feedback

Where do I belong - feedback
0

#1

Hi Everyone,

I finish this challenge, but I want to know Is this a proper way, or there is a better way?

Thanks

function getIndexToIns(arr, num) {
  var b = Array.prototype.slice.call(arguments,1);
  var c = b.concat(arr);
  c.sort(function(a,b) {
    return a - b;
  });
  return  c.indexOf(num);
}

getIndexToIns([40, 60], 50);

#2

Hi, looks good. You can compare your solution with the wiki.


#3

Got it in the 1st Shot :sunglasses:


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

#4

There is an easier way to put a new element into an array than to use .slice on arguments. You could just use .push to add it to the end.

But there is no real need to either put the element in or to sort it. We can just loop over the array and check how many elements are less than num.


#5

you don’t even need to sort the array.

btw, .sort() changes the array in place, so the assignment doesn’t really matter, as you change the original either way.


#6

This thread should be closed.


#8

I reached the solution this way, and I’m just curious to know if this is an appropriate method to reaching the solution or if there is some reason any of the other solutions would be better?

function getIndexToIns(arr, num) {
var concatArr = arr.concat(num).sort(function(a,b){
return a-b;
});
return concatArr.indexOf(num);
}