简体   繁体   English

为什么Eloquent JavaScript中的此函数示例返回值24?

[英]Why does this function example from Eloquent JavaScript return the value 24?

I am new to JavaScript and am trying to understand an example from the book. 我是JavaScript的新手,正试图从书中了解一个示例。 When I step through this example, I expect the value of fac(4) to be 12. That would be the result of (4 - 1) * 4. 当我逐步执行此示例时,我期望fac(4)的值为12。这是(4-1-1)* 4的结果。

However, when I run the code from the online book (near bottom of page) at http://eloquentjavascript.net/00_intro.html , I get the value 24. What am I missing here? 但是,当我从位于http://eloquentjavascript.net/00_intro.html的在线图书(页面底部)运行代码时,得到的值是24。这里缺少什么?

function fac(n) {
  if (n == 0)
    return 1;
  else
    return fac(n - 1) * n;
}
console.log(fac(4));

I have tried to find an answer through Twitter and through online research but have been unsuccessful. 我试图通过Twitter和在线研究找到答案,但没有成功。 I know I am overlooking something, but can't seem to see it. 我知道我正在俯瞰某些事物,但似乎看不到它。

Because it is recursive. 因为它是递归的。

function fac(n) {
  if (n == 0)
    return 1;
  else
    return fac(n - 1) * n;
}
console.log(fac(4));

So let's trace it 所以让我们追踪一下

fac(3) * 4;
fac(2) * 3 * 4;
fac(1) * 2 * 3 * 4;
fac(0) * 1 * 2 * 3 * 4;
1 * 1 * 2 * 3 * 4;

And of course, that is 24. 当然是24

If you notice your code, at line 5 this function is calling itself, this is called recursion, read more about it on wikipedia 如果您注意到您的代码,则在第5行,此函数正在调用自身,这称为递归,请在Wikipedia上了解有关它的更多信息

Lets comeback to your code and dry run it to see how it works. 让我们回到您的代码并对其进行空运行以查看其工作方式。 lets give each line a number so we can refer it 让我们给每行一个数字,以便我们可以参考它

1.function fac(n) {
2.  if (n == 0)
3.    return 1;
4.  else
5.    return fac(n - 1) * n;
6.}
7.console.log(fac(4));

your input value is 4 for first call lets call it call A . 您的第一个呼叫输入值为4,我们称其为call A

for value 4 if condition at line 1 will return false so it would go to line 5 which again calls 对于值4, if第1行的条件将返回false,那么它将转到第5行,再次调用

fac(3) * 4;

now your function call A is paused here and makes a call B with value 3. Result of call A is yet to be evaluated. 现在,您的函数call A暂停在此处,并使用值3进行了call B call A结果尚未评估。

Now with value 3 this if condition at line 2 will return false again and code at line 5 will execute like above; 现在,值为3时, if第2行的条件再次返回false ,则第5行的代码将像上面一样执行; ie

fac(2) * 3;

call B paused here and there will be call C with input 2 of the same function. call B在这里暂停,并且具有相同功能的输入2的call C

on call C you code will return false against if condition at line 2 so it will make call D with input 1. ie call C ,代码将针对第2行的if条件返回false ,因此它将使用输入1进行call D

fac(1) * 2

For call D if condition at line 2 will return true and your function will return 1 so the call D gets evaluated at with value 1. now replace each fac(n) from bottom to top and keep on evaluating each call. 对于call D if第2行的条件将返回true,并且您的函数将返回1,则call D值将为1。现在,从下到上替换每个fac(n)并继续评估每个调用。

result of call D => 1  
result of call C => fac(1) * 2 => 1 * 2 => 2  
result of call B => fac(2) * 3 => 2 * 3 => 6  
result of call A => fac(3) * 4 => 6 * 4 => 24

I hope you will be able to understand how it really works, important point in recursion is the stop condition which you code has it at line 2. 我希望您能够理解它是如何工作的,递归的重点是停止条件,您的代码在第2行将其停止。

Above result can also be produced by using loops but using recursion is more fun. 通过使用循环也可以产生以上结果,但是使用递归会更有趣。

Happy Coding :) 快乐编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM