JavaScript中递归和执行流程的更清晰的解释? [英] A clearer explanation for recursion and flow of execution in JavaScript?
问题描述
我正在阅读Eloquent JavaScript,我遇到了这个难题的例子:
I was reading Eloquent JavaScript and I came across this example for the puzzle:
考虑这个难题:从$ b $开始b数字1并重复
加5或乘以3,
无限数量的新数字可以产生
。你会如何编写一个
函数,给定一个数字,尝试
来查找一系列加法和
乘法产生
数?
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));
如果没有参数的值,有人可以清理dod find如何执行和目标?递归是怎么发生的?
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?
推荐答案
但是找到
没有 start
和目标
的值,没有执行。它首先使用 1
的值执行 start
,并且目标的唯一值
24
。
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
.
也许您对操作顺序感到困惑。在那里我们看到函数的声明, findSequence
。在声明期间,不执行任何代码。 findSequence
函数仅在最后一行执行,执行函数的结果将被打印出来。
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.
在 findSequence
的声明中,有另一个函数的声明, find
。再一次,直到后来它才会被执行。 findSequence
函数只有一个可执行代码行,一个调用 find(1,1)
。执行该一行会以递归的方式触发 find
的执行次数。 find
函数引用目标
;当Javascript解释器执行代码时, goal
总是引用 findSequence
的参数,因为在此示例中 findSequence
仅调用一次,目标
始终具有相同的值, 24
。
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
.
您应该能够看到递归发生的位置。如果 start
等于 goal
,则函数停止;它返回它到达该数字的历史。如果 start
大于 goal
,则返回 null
,表示该路径不是目标号码的路径。如果 start
仍然小于 goal
,那么该函数会尝试调用自身 value plus 5.如果返回非null值,那么返回的是。否则,它会尝试乘以3并返回该历史值。
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.
请注意,尽管此代码可以返回许多数字,但它无法返回 all 数字。如果目标是 2
,例如, findSequence
将返回 null
因为无法从 1
开始,并通过添加 5获得
或乘以 2
3
。
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屋!