没有得到正确的二进制搜索算法

Not getting the binary search algorithm correct

我正在尝试实施 binary search 并执行了以下操作:

function bs(a,x) {
    // a : array to look into
    // x : number to find
    let mpoint = Math.floor(a.length / 2);
    if(x >= a[mpoint]) {
        if(x == a[mpoint]) { return mpoint;}
        else {
            return bs([...a].slice(mpoint,a.length), x)
        }
    }else {
        if(x == a[mpoint]) {return mpoint;}
        else {
            return bs([...a].slice(0,mpoint),x)
        }
    }
}


bs([ 2, 3, 4, 10, 40 ], 10)

但是我得到了一个不正确的 index 结果。我做错了什么?

尝试改变:

return bs([...a].slice(mpoint,a.length), x)

至:

return bs([...a].slice(mpoint,a.length), x) + mpoint