对 JavaScript 中的递归和执行流程有更清晰的解释吗? [英] A clearer explanation for recursion and flow of execution in JavaScript?

查看:20
本文介绍了对 JavaScript 中的递归和执行流程有更清晰的解释吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阅读 Eloquent JavaScript 时遇到了这个谜题示例:

<块引用>

考虑这个难题:从数字 1 并重复加 5 或乘以 3,一个无限数量的新数字可以是产生.你会如何写一个函数,给定一个数字,尝试找到一系列的加法和产生那个的乘法号码?

这是解决方案的代码:

function findSequence(goal) {函数查找(开始,历史){如果(开始==目标)返回历史;否则如果(开始>目标)返回空;别的return find(start + 5, "(" + history + " + 5)") ||find(start * 3, "(" + history + " * 3)");}返回查找(1,1");}打印(查找序列(24));

如果 find 没有参数 start 和目标的值,有人能弄清楚它是如何执行的吗?还有递归是怎么发生的?

解决方案

但是 find didn't 在没有 start 值的情况下被执行和目标.它首先使用 start 的值 1 执行,goal 的唯一值是 24.>

也许您对操作顺序感到困惑.在那里我们看到了一个函数的声明findSequence.在声明期间,不执行任何代码.findSequence 函数只在稍后执行,在最后一行,执行函数的结果被打印出来.

findSequence 的声明中,还有另一个函数find 的声明.再一次,它直到稍后才会被执行.findSequence 函数只有一行 可执行代码,即调用find(1, "1") 的那一行.执行那一行会递归地触发 find 的执行几次.find 函数引用了goal;当Javascript解释器执行代码时,goal总是引用findSequence的参数,因为在这个例子中findSequence只被调用了一次,goal 始终具有相同的值,24.

您应该能够看到递归发生的位置.如果start 等于goal,则函数停止;它返回它如何到达该数字的历史记录.如果start大于goal,则返回null,表示该路径不是到达目标编号的路径.如果 start 仍然小于 goal,则函数尝试调用自身,其起始值加 5.如果返回非空值,那么这就是返回的内容.否则,它会尝试乘以 3 并返回该历史值.

请注意,虽然此代码可以返回多个数字,但它不能返回所有数字.如果目标是2,例如findSequence会返回null,因为没有办法从1开始并通过添加 5 或乘以 3 得到 2.

I was reading Eloquent JavaScript and I came across this example for the puzzle:

Consider this puzzle: By starting from the number 1 and repeatedly either adding 5 or multiplying by 3, an infinite amount of new numbers can be produced. How would you write a function that, given a number, tries to find a sequence of additions and multiplications that produce that number?

Here's the code for the solution:

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

print(findSequence(24));

Could someone clear up how dod find get executed if it didn't have a value for the arguments start and goal? Also how did the recursion happen?

解决方案

But find didn't get executed without a value for start and goal. It was first executed with the value 1 for start, and the only value for goal was 24.

Perhaps you're confused about the order of operations. There we see the declaration of a function, findSequence. During the declaration, no code is executed. The findSequence function only gets executed later, on the last line, where the result of executing the function gets printed out.

Within the declaration of findSequence, there's a declaration of another function, find. Once again, it doesn't get executed until later. The findSequence function has just one executable line of code, the one that calls find(1, "1"). Execution of that one line triggers the execution of find some number of times, recursively. The find function makes reference to goal; when the Javascript interpreter executes the code, goal always refers to the parameter of findSequence, and since in this example findSequence is only called once, goal always has the same value, 24.

You should be able to see where the recursion happened. If start was equal to goal, then the function stops; it returns the history of how it arrived at that number. If start is greater than goal, then it returns null, indicating that that path was not a path to the target number. If start is still less than goal, then the function tries calling itself with its start value plus 5. If that returns a non-null value, then that's what gets returned. Otherwise, it tries multiplying by 3 and returning that history value instead.

Note that although this code can return many numbers, it cannot return all numbers. If the goal is 2, for example, findSequence will return null because there is no way to start at 1 and get to 2 by adding 5 or multiplying by 3.

这篇关于对 JavaScript 中的递归和执行流程有更清晰的解释吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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