为什么我的算法返回“-1”意味着目标值 73 不在数组中?(当显然 73 在数组中时)。[这是来自可汗学院,但没有帮助]
它应该返回数组中位置的索引,或者如果数组不包含 targetValue 则返回“-1”
Var doSearch = function(array, targetValue) {
var min = 0;
var max = array.length - 1;
var guess;
while(max >= min) {
guess = floor((max*1 + min*1) / 2);
if (guess === targetValue) {
return guess;
} else if (guess < targetValue) {
min = guess + 1;
} else {
max = guess - 1;
}
}
return -1;
};
var primes = [2, 3, 5, 7, 11, 13, 17, 19, …Run Code Online (Sandbox Code Playgroud)