JavaScript 中递归函数的 return 语句如何返回值? [英] How does return statement return values in recursive functions in JavaScript?

查看:80
本文介绍了JavaScript 中递归函数的 return 语句如何返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下这个递归函数是如何工作的.我无法理解如何在调用堆栈中返回值.你能不能用一些图形来解释,代表执行上下文.如果你能像 Udemy 课程 JavaScript-理解奇怪的部分中解释的那样解释方式会更好.

function findSolution(target) {函数查找(开始,历史){如果(开始==目标)返回历史;否则如果(开始>目标)返回空;别的return find(start + 5, "(" + history + " + 5) ") ||find(start * 3, "(" + history + " * 3) ");}返回查找(1,1");}console.log(findSolution(13));

解决方案

你所拥有的是一个递归函数,它有两个退出条件,一个用于找到结果,其中起始值和目标值相等,返回历史和一个其中起始值大于目标值,未找到结果返回null.

递归机制是将函数的调用分为两个方向,一个是加fice,另一个是将值乘以3.

分支发生在 logical OR ||,这意味着如果第一个分支返回 truthy 结果,这个结果被采用并且另一个分支永远不会被调用.如果第一个分支返回 null,一个 falsy 值,调用第二个分支.

你最终得到的是一棵带有分支和测试的树.

 1 级别 0/\2 5 级 1/\/\3 4 6 7 级别 2

这意味着,在第一次调用时,它从 1 开始,必要时转到 2 和 3,依此类推.

要获得一个想法,您可以将级别变量添加到 console.log 并观察每个级别的情况.

<块引用>

关卡、开始、历史动作----------------------------------------------- ----------------0 1 1 检查下一级1 6 (1 + 5) 检查下一级2 11 ((1 + 5) + 5) 检查下一层3 16 (((1 + 5) + 5) + 5) 空3 33 (((1 + 5) + 5) * 3) 空2 18 ((1 + 5) * 3) 空1 3 (1 * 3) 检查下一级2 8 ((1 * 3) + 5) 检查下一级3 13 (((1 * 3) + 5) + 5) 找到(((1 * 3) + 5) + 5) 结果

function findSolution(target) {函数查找(开始,历史,级别){级别 = 级别 ||0;控制台日志(级别,开始,历史);if (start == target) 返回历史记录;//退出条件 1(找到)if (start > target) return null;//退出条件 2(未找到)return find(start + 5, "(" + history + " + 5) ", level + 1) ||find(start * 3, "(" + history + " * 3) ", level + 1);}返回查找(1,1");}console.log(findSolution(13));

.as-console-wrapper { max-height: 100% !important;顶部:0;}

Can someone please explain me how this recursive function works. I can't understand how the values are returned in call stack. Could you please explain with some graphics, representing the execution contexts. It would be better if you can explain the way as explained in the Udemy course JavaScript-understanding the weird parts.

function findSolution(target) {

    function find(start, history) {
        if (start == target)
            return history;
        else if (start > target)
           return null;
        else
            return find(start + 5, "(" + history + " + 5) ") || find(start * 3, "(" + history + " * 3) ");
    }

return find(1, "1");
}
console.log(findSolution(13));

解决方案

What you have is a recursive function with two exit conditions, one for a found result, where the start and target value is equal, this returns the history and one where the start value is greater than the target value, which returns null for not found result.

The recursive machanis is to branch the calling of the function in two direction, one with adding fice, the other one by multiplying the value by three.

The branching takes place with a logical OR ||, which means if the first branch returns a truthy result, this result is taken and the other branch is never called. If the first branch returns null, a falsy value, the second branch is called.

What you finally get is a tree with branches and tests.

      1          level 0
    /   \
  2       5      level 1
 / \     / \
3   4   6   7    level 2

That means, with the first call, it starts with 1 and goes if necessary to 2 and 3 and so on.

To get an idea, you could add a level variable to a console.log and watch what is going in every level.

level, start, history                         action
--------------------------------------------  ----------------
0 1 1                                         check next level
    1 6 (1 + 5)                               check next level
        2 11 ((1 + 5)  + 5)                   check next level
            3 16 (((1 + 5)  + 5)  + 5)        null
            3 33 (((1 + 5)  + 5)  * 3)        null
        2 18 ((1 + 5)  * 3)                   null
    1 3 (1 * 3)                               check next level
        2 8 ((1 * 3)  + 5)                    check next level
            3 13 (((1 * 3)  + 5)  + 5)        found

(((1 * 3)  + 5)  + 5)                         result

function findSolution(target) {

    function find(start, history, level) {
        level = level || 0;
        console.log(level, start, history);
        if (start == target) return history;            // exit condition 1 (found)
        if (start > target) return null;                // exit condition 2 (not found)
        return find(start + 5, "(" + history + " + 5) ", level + 1) ||
               find(start * 3, "(" + history + " * 3) ", level + 1);
    }

    return find(1, "1");
}
console.log(findSolution(13));

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于JavaScript 中递归函数的 return 语句如何返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆