小编Bri*_*aux的帖子

为什么我的算法找不到数组中的索引?

为什么我的算法返回“-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)

javascript binary-search khan-academy

2
推荐指数
1
解决办法
60
查看次数

标签 统计

binary-search ×1

javascript ×1

khan-academy ×1