javascript中的递归和返回值

javascript中的递归和返回值,javascript,recursion,Javascript,Recursion,我最近一直在练习用JavaScript编写递归函数,当涉及到返回值时,我一直遇到一个问题,即当从条件块内部返回值时,它们似乎总是未定义 下面是一个关于JSFIDLE的示例 这是一个递归实现的简单二进制排序。在这段代码中 if (array[guess] === targetValue){ console.log("equal"); return(guess); } 我在控制台中看到“平等”,除非 A) The variable guess is defined

我最近一直在练习用JavaScript编写递归函数,当涉及到返回值时,我一直遇到一个问题,即当从条件块内部返回值时,它们似乎总是未定义

下面是一个关于JSFIDLE的示例

这是一个递归实现的简单二进制排序。在这段代码中

if (array[guess] === targetValue){
    console.log("equal");
    return(guess);
}
我在控制台中看到“平等”,除非

    A) The variable guess is defined
    B) The item located at the index of that array equals the target search value
所以我知道guess是绝对定义的,否则我的函数永远不会计算console.log语句,但返回的值总是未定义的


我错过了什么?关于递归,我不了解的是什么?这是我在过去两周内编写的第三个函数,其行为方式与此相同,我无法在任何在线位置找到任何类型的答案。

您需要返回对递归函数的调用,否则无法传递值。例如:

// Added: return doSearch(array, targetValue);

let doSearch = function(array, targetValue) {
    let min = 0;
    let max = array.length - 1;
    let guess = Math.floor((max + min) / 2);

    if (array[guess] === targetValue) {
        console.log("equal");
        return guess;
    } else if (array[guess] > targetValue) {
        array = array.slice(0, guess);
        return doSearch(array, targetValue);
    } else if (array[guess] < targetValue) {
        array = array.slice(guess + 1);
        return doSearch(array, targetValue);
    } else {
        return "not in the list";
    }
};
//添加:返回doSearch(数组,targetValue);
let doSearch=函数(数组,targetValue){
设min=0;
设max=array.length-1;
让猜测=数学楼层((最大+最小)/2);
if(数组[猜测]==targetValue){
控制台日志(“相等”);
返回猜测;
}else if(数组[猜测]>targetValue){
array=array.slice(0,猜测);
返回doSearch(数组,targetValue);
}else if(数组[猜测]

另外,我怀疑您希望在最终返回中返回
returnarray[guess]
,它应该给出数字。当您在每个递归上拆分数组时,
guess
的值将变得没有意义。

您需要返回对递归函数的调用,否则这些值将无法传递。例如:

// Added: return doSearch(array, targetValue);

let doSearch = function(array, targetValue) {
    let min = 0;
    let max = array.length - 1;
    let guess = Math.floor((max + min) / 2);

    if (array[guess] === targetValue) {
        console.log("equal");
        return guess;
    } else if (array[guess] > targetValue) {
        array = array.slice(0, guess);
        return doSearch(array, targetValue);
    } else if (array[guess] < targetValue) {
        array = array.slice(guess + 1);
        return doSearch(array, targetValue);
    } else {
        return "not in the list";
    }
};
//添加:返回doSearch(数组,targetValue);
let doSearch=函数(数组,targetValue){
设min=0;
设max=array.length-1;
让猜测=数学楼层((最大+最小)/2);
if(数组[猜测]==targetValue){
控制台日志(“相等”);
返回猜测;
}else if(数组[猜测]>targetValue){
array=array.slice(0,猜测);
返回doSearch(数组,targetValue);
}else if(数组[猜测]

另外,我怀疑您希望在最终返回中返回
returnarray[guess]
,它应该给出数字。当您在每个递归上拆分数组时,
guess
的值将变得没有意义。

您缺少几个
return
语句。当您执行递归调用并返回值时,您需要使用该值执行某些操作;在这种情况下,请将其返回。这就是您得到未定义的
的原因。代码完全按照您的要求执行,查找项,但从未将其传递回调用堆栈

编辑:其次,(我忽略了这一点),因为您正在对数组进行切片,所以切片数组中的解决方案索引不一定与源数组中的索引相同,因此当它发生移位时,您应该对此进行调整

编辑2:我建议使用像
-1
这样的通用sentinel值,而不是在值不在数组中时使用的字符串

let doSearch=函数(数组,targetValue){
设min=0;
设max=array.length-1;
让猜测=数学楼层((最大+最小)/2);
if(数组[猜测]==targetValue){
控制台日志(“相等”);
返回猜测;
}else if(数组[猜测]>targetValue){
array=array.slice(0,猜测);
返回doSearch(数组,targetValue);
}else if(数组[猜测]
您缺少几条
return
语句。当您执行递归调用并返回值时,您需要使用该值执行某些操作;在这种情况下,请将其返回。这就是您得到未定义的
的原因。代码完全按照您的要求执行,查找项,但从未将其传递回调用堆栈

编辑:其次,(我忽略了这一点),因为您正在对数组进行切片,所以切片数组中的解决方案索引不一定与源数组中的索引相同,因此当它发生移位时,您应该对此进行调整

编辑2:我建议使用像
-1
这样的通用sentinel值,而不是在值不在数组中时使用的字符串

let doSearch=函数(数组,targetValue){
设min=0;
设max=array.length-1;
让猜测=数学楼层((最大+最小)/2);
if(数组[猜测]==targetValue){
控制台日志(“相等”);
返回猜测;
}else if(数组[猜测]>targetValue){
array=array.slice(0,猜测);
返回doSearch(数组,targetValue);
}else if(数组[猜测]
您需要使用
返回
。如果需要原始索引,可能会更改代码以跟踪最小值和最大值:

let doSearch = function(array, minI, maxI, targetValue) {
    let guess = Math.floor((maxI + minI)/2);
    if(array[guess] === targetValue) {
      console.log("equal");
      return guess;
    } else if (minI === maxI) {
      console.log("Not in list")
      return
    } else if (array[guess] > targetValue) {
      return doSearch(array, minI, guess, targetValue);
    } else if (array[guess] < targetValue) {
      return doSearch(array, guess+1, maxI, targetValue);
    }
};

let primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 
        41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];

let result = doSearch(primes, 0, primes.length-1, 71);
let doSearch=函数(数组、最小值、最大值、targetValue){
让猜测=数学楼层((最大+最小)/2);
if(数组[猜测]==targetValue){
控制台日志(“相等”);
返回猜测;
}else if(minI==maxI){
console.log(“不在列表中”)
返回
}else if(数组[猜测]>targetValue){
返回doSearch(数组、迷你、猜测、targetValue);
}else if(数组[猜测]