简体   繁体   English

你能为我解释一下这个 JavaScript function 吗?

[英]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 ,它将从1n的所有数字放入一个数组中。 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.这会发生多次,直到n0 ,在这种情况下,第一个 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.

相关问题 您能向我解释这个Javascript回调函数吗? - Can you explain this Javascript callback function to me? 有人可以解释一下 javascript 中的这个箭头 function 吗? - Can sombody explain me this arrow function in javascript? 您可以向我解释一下Javascript Cookie教程的这一行代码吗? - Can you explain to me about this line of code for Javascript cookie tutorial? 有人可以向我解释此JavaScript函数的流程吗? (关闭概念) - Can someone explain me the flow of this JavaScript function? (Closure concept) 任何人都可以从 eloquent javascript 中向我解释以下函数 - Can anyone please explain me the following function from eloquent javascript 你能解释一下这个反应 function 吗? - Can you explain this react function? 您能为我解释一下我不了解的功能,以及如何将其转换为“正常”功能吗? - Can you explain me this function I didn't understand and how to convert it in “normal” function? 有人可以向我解释此Javascript Singleton代码吗? - Can someone explain this Javascript Singleton code to me? 有人可以向我解释这种JavaScript行为吗? isNaN([]) - Can someone explain me this javascript behaviour? isNaN([]) 有人可以向我解释这个HTML / Javascript部分吗? - Can someone explain to me this HTML / Javascript section?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM