[英]Can you explain this JavaScript function for me?
function countup(n) {
if (n < 1) {
return [];
} else {
const countArray = countup(n - 1);
countArray.push(n);
return countArray;
}
}
console.log(countup(5)); // [ 1, 2, 3, 4, 5 ]
Could you guys please explain to me how this function works and when the variable "countArray" would push the elements to the array.你们能否向我解释一下这个 function 是如何工作的,以及变量“countArray”何时会将元素推送到数组中。
It is a recursive function that puts all numbers from 1
to n
into an array.它是一个递归 function ,它将从
1
到n
的所有数字放入一个数组中。 Lets look at the example with n = 5
:让我们看一下
n = 5
的示例:
you enter the function and check if n
is smaller than 1
, as this is not the case, you jump into the else statement, where the array countArray
is initialized with the value of countup(n - 1)
, therefore you start from the beginning of the function, but with n = 4
.你输入 function 并检查
n
是否小于1
,因为不是这种情况,你跳到 else 语句,其中数组countArray
用 countup countup(n - 1)
的值初始化,因此你从头开始function,但n = 4
。
This happens multiple times, until n
is 0
, as in that case, the first if statement is true.这会发生多次,直到
n
为0
,在这种情况下,第一个 if 语句为真。 By this exit condition, the function returns an empty array, which is assigned to the variable countArray
in the function call in the previous 'layer' of recursion.通过此退出条件,function 返回一个空数组,该数组被分配给前一个递归“层”中 function 调用中的变量
countArray
。 After that, the variable n
which equals 1
on that 'layer' is pushed into the array.之后,将该“层”上等于
1
的变量n
推入数组中。 These last steps also happen multiple times, as countArray
is returned to the next call of the recursive function, in the same way as the empty array was returned.最后这些步骤也会发生多次,因为
countArray
返回到递归 function 的下一次调用,与返回空数组的方式相同。
After all recursive function calls were executed the resulting array [1,2,3,4,5]
is returned在执行所有递归 function 调用后,返回结果数组
[1,2,3,4,5]
This function will check first if the variable n < 1
这个 function 将首先检查变量
n < 1
If it is bigger it will run itself with n-1
如果它更大,它将以
n-1
自行运行
If n
is 0
, the function will return an empty Array and will go one "layer" back to the last function countup(1)
It will now add n (1)
to the countArray and return this Array to the "last" function Now the function countup(2)
has recieved the number 1
in its countArray
and it will return this. If
n
is 0
, the function will return an empty Array and will go one "layer" back to the last function countup countup(1)
It will now add n (1)
to the countArray and return this Array to the "last" function Now function countup countup(2)
已在其countArray
中收到数字1
,它将返回此数字。 [1, 2]
It will repeate this process until the 'last' / 'first' function ( countup(5)
) is reached and it will return the countArray
with all the numbers [1, 2, 3, 4, 5]
它将重复此过程,直到达到“最后一个”/“第一个” function ( countup
countup(5)
) 并将返回包含所有数字[1, 2, 3, 4, 5]
的countArray
To understand recursions, sometimes you need to follow the execution of the code manually.要理解递归,有时您需要手动跟踪代码的执行。 Now the following line causes the recursive call and subtracts one from the number and then passes it to the recursive call.
现在下面的行导致递归调用并从数字中减一,然后将其传递给递归调用。
const countArray = countup(n - 1);
Now imagine, if you passed 5 to the initial function call like so:现在想象一下,如果您将 5 传递给最初的 function 调用,如下所示:
countup(5);
As 5 is not less than 1, the else
block will be executed.由于 5 不小于 1,
else
块将被执行。 The first line in the else block is the recursive call. else 块中的第一行是递归调用。 So when your program encounters the recursive call, it stops execution of the current function until that recursive function call is concluded/returned.
因此,当您的程序遇到递归调用时,它会停止当前 function 的执行,直到该递归 function 调用结束/返回。 So the first recursive call will be:
所以第一个递归调用将是:
const countArray = countup(4);
consequently, the same process will be repeated until n
equals 0
.因此,将重复相同的过程,直到
n
等于0
。 As 0 is smaller than 1, the if condition will evaluate to true
.由于 0 小于 1,if 条件将评估为
true
。 and it will return an empty array
.它将返回一个
empty array
。
After the recursive call, it pushes the number to the array returned by recursive call.递归调用后,将数字推送到递归调用返回的数组中。 so the array at that point will contain
[0]
and then [0, 1]
and it will keep adding numbers till the all recursive calls have been computed and then it will execute the rest of the intial function call.所以该点的数组将包含
[0]
然后[0, 1]
并且它将继续添加数字,直到计算出所有递归调用,然后它将执行初始 function 调用的 rest。
Often times, recursion can be replaced by loops but thats not the case always.很多时候,递归可以被循环代替,但情况并非总是如此。 As a programmer one should be aware of this important topic.
作为一名程序员,应该意识到这个重要的话题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.