简体   繁体   English

javascript递归函数-不了解发生了什么

[英]javascript recursive function - not understanding what is going on

I was reading javascript documentation on 'functions' and came across this code that I am not following how it's working step by step, specifically the inner recursive function. 我正在阅读有关“函数”的javascript文档,并遇到了这段代码,但我没有逐步关注它的工作方式,尤其是内部递归函数。

function factorial(n){
  if ((n == 0) || (n == 1))
    return 1;
  else
    return (n * factorial(n - 1));
}

var a, b, c, d, e;
a = factorial(1); // a gets the value 1
b = factorial(2); // b gets the value 2
c = factorial(3); // c gets the value 6
d = factorial(4); // d gets the value 24
e = factorial(5); // e gets the value 120

I am not following the logic beyond the first if statement. 我没有遵循第一个if语句之外的逻辑。 Could someone spell it out. 有人可以说出来吗? I have already ran the code and works just as specified. 我已经运行了代码并按照指定的方式工作。

For example, let's calculate factorial(4) : 例如,让我们计算factorial(4)

  • n = 4
  • Since n != 0 and n != 1 , the value of factorial(4) is 4 * factorial(3) 由于n != 0n != 1 ,因此factorial(4)值为4 * factorial(3)
  • Let's calculate factorial(3) 让我们计算factorial(3)
    • n = 3
    • Since n != 0 and n != 1 , the value of factorial(3) is 3 * factorial(2) 由于n != 0n != 1 ,因此factorial(3)值为3 * factorial(2)
    • Let's calculate factorial(2) 让我们计算factorial(2)
      • n = 2
      • Since n != 0 and n != 1 , the value of factorial(2) is 2 * factorial(1) 由于n != 0n != 1 ,因此factorial(2)值为2 * factorial(1)
      • Let's calculate factorial(1) 让我们计算factorial(1)
        • n = 1
        • Since n == 1 , the value of factorial(1) is 1 由于n == 1 ,所以factorial(1)值为1
      • Therefore, the value of factorial(2) is 2 * 1 == 2 因此, factorial(2)值为2 * 1 == 2
    • Therefore, the value of factorial(3) is 3 * 2 == 6 因此, factorial(3)值为3 * 2 == 6
  • Therefore, the value of factorial(4) is 4 * 6 == 24 因此, factorial(4)值为4 * 6 == 24

If the passed parameter is 0 or 1, 1 is returned. 如果传递的参数是0或1,则返回1。

If the passed parameter is 2, then the parameter (2) multiplied by a recursion of the same function being passed 1, since it's passed 1, 1 is returned, as above. 如果传递的参数为2,则参数(2)乘以传递1的相同函数的递归,因为传递了1,所以如上所述返回1。

If the parameter passed is 3, then it becomes 3 * 2 * 1, the latter two being resolved through recursion. 如果传递的参数为3,则它变为3 * 2 * 1,后两个通过递归解决。

For 4, then it becomes 4 * 3 * 2 * 1, the latter 3 being resolved through recursion. 对于4,则变为4 * 3 * 2 * 1,后3个通过递归解决。

Thus if you were wondering what 100 * 99 * 98..... * 3 * 2 * 1 equaled, this function could provide the answer. 因此,如果您想知道100 * 99 * 98 ..... * 3 * 2 * 1等于什么,此函数可以提供答案。

The point of the if statement, of course, is to make sure that factorial(1 - 1) never happens, because if it did, the last step would be * 0, making the result always 0. 当然,if语句的要点是确保阶乘(1-1)永远不会发生,因为如果这样做,那么最后一步将是* 0,使结果始终为0。

As @Oriol points out in reply to me (and in their own answer), the point is also (but primarily) to make sure the recursion stops, otherwise it would continue to negative infinity (or rather, system crash). 正如@Oriol在回复我(以及他们自己的回答)中指出的那样,重点(主要是)是确保递归停止,否则它将继续为负无穷大(或更确切地说,系统崩溃)。

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

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