<script>
function twoIndex(val, arr) {
var start = 0,
end = arr.length - 1,
middle = Math.floor((start + end) / 2);
while (arr[middle] !== val && end > start) {
arr[middle] > val ? (end = middle - 1) : (start = middle + 1);
middle = Math.floor((start + end) / 2);
}
return (arr[middle] !== val) ? -1 : middle;
}
console.log(twoIndex(3, [1,2,3,4,5,6,7,8,9]));
</script>
时间: 2024-11-06 07:19:33