## 代码实现
利用二分查找,返回一个数在数组的索引位置,必须是有序数组
```
let arr = [1, 2, 3, 4, 6, 7, 8];
function test(arr, val) {
let low = 0;
let high = arr.length - 1;
while (low <= high) {
let mid = parseInt( (low + high) / 2 );
if(val == arr[mid]){
return mid;
} else if(val > arr\[mid\]){
low = mid + 1;
} else if (val < arr\[mid\]){
high = mid - 1;
}
}
return -1;
}
test(arr, 5);
```