# Where do I belong Algorithm refractor

Where do I belong Algorithm refractor
0
#1

Can someone out there help me refractor the “Where do I belong” algorithm?

Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.

Here is my code:

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

}
function compareNumbers(a, b){
return a-b;
}

getIndexToIns([40, 60], 50);

1 Like

#3

you are so close you have nearly everything plus stuff you dont need … but what you do require is wrongly excuted.[quote=“joelc316, post:1, topic:26653”]
function compareNumbers(a, b){ return a-b;
[/quote]

change to function(a,b){return a-b;} put this in the arr.sort() and thats all you need
eg arr.sort(function(a,b){return a-b;}) (this sorts the numbers … you just applied it wrong)
all you need then is to find where num is in the arr
return arr.indexOf(num);

0 Likes