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);
Hi, looks good. You can compare your solution with the wiki.
Got it in the 1st Shot 
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;
}
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.
1 Like
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.
This thread should be closed.
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);
}