Skip to content

Instantly share code, notes, and snippets.

@JiLiZART
Last active June 27, 2016 00:08
Show Gist options
  • Save JiLiZART/57daaf62bc1e8542b56b3e040e911f13 to your computer and use it in GitHub Desktop.
Save JiLiZART/57daaf62bc1e8542b56b3e040e911f13 to your computer and use it in GitHub Desktop.
function get_pos(arr, number) {
var l = 0,
r = arr.length,
m;
while (l < r) {
m = Math.floor( (l + r) / 2 );
if (number < arr[m]) {
r = m;
} else if (number > arr[m]) {
l = m + 1;
} else {
return m + 1;
}
}
return -1;
}
var some = [11, 22, 33, 44, 55, 128, 256];
console.log(get_pos(some, 128));
console.log(get_pos([128], 128));
console.log(get_pos([], 128));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment