Skip to content

Instantly share code, notes, and snippets.

@bflannery
Created December 27, 2016 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bflannery/374ddf3f9a7763793f08a89d963e11c3 to your computer and use it in GitHub Desktop.
Save bflannery/374ddf3f9a7763793f08a89d963e11c3 to your computer and use it in GitHub Desktop.
Where Do I Belong
// 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.
function getIndexToIns(arr, num) {
arr.sort((a,b)=>{
return a - b;
});
for(var i in arr) {
if(arr[i] >= num)
return parseInt(i);
}
return arr.length;
}
console.log(getIndexToIns([5, 3, 20, 3], 5));
console.log(getIndexToIns([40, 60], 50));
console.log(getIndexToIns([2, 5, 10], 15));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment