Created
May 15, 2016 14:11
-
-
Save shelldandy/532b389b1989759cf0b3b95213a9863a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getIndexToIns(arr, num) { | |
var sort = function(a,b) { | |
return a - b; | |
}; | |
var sortedArray = arr.sort(sort); | |
var length = sortedArray.length; | |
if(num <= sortedArray[0]) { | |
return 0; | |
} | |
else { | |
// start with the answer | |
var answer = 1; | |
for(answer; answer < length; answer++) { | |
if( num <= sortedArray[answer]) { | |
return answer; | |
} | |
} | |
return answer; | |
} | |
} | |
getIndexToIns( [10, 20, 30, 40, 50], 30 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment