Created
December 27, 2016 21:13
-
-
Save bflannery/374ddf3f9a7763793f08a89d963e11c3 to your computer and use it in GitHub Desktop.
Where Do I Belong
This file contains 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
// 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