JS中的递归计数到达0后会发生什么?
Recursive count in JS what happen after reaching 0?
不幸的是,尽管阅读了我在网上找到的内容,但我仍然不明白它是如何工作的。
这是代码:
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 ]
我理解的最简单的部分是用 n-1 调用函数直到它达到 0 然后将 [] 推到 countArray 常量。
但后来我明白了它是如何将 1 然后 2,然后 3 等作为 n = 0 到 countArray 常量的,然后什么都不应该发生,因为不再调用该函数。
有人可以帮助我吗?
由于您还没有掌握函数调用的窍门,因此在深入研究递归之前,您必须首先完全理解以下程序。我也添加了完整的评论。这里我们从另一个调用一个函数。递归也是一样的,但是同样的函数被自己调用。所以首先要了解以下内容:-
function function1(a,b)
{
let result1 = function2(a,b);
return result1 + a + b;
}
function function2(a,b){
let result2 = function3(a,b);
return result2 * a * b;
}
function function3(a,b){
let result3 = function4(a,b);
return result3 - a - b;
}
function function4(a,b){return a/b}
console.log(function1(4,2))
// It will work like this :-
/*
function1 gets called with a=2,b=4
function2 gets called with a=2,b=4
function3 gets called with a=2,b=4
function4 gets called with a=2,b=4 and doesn't call anymore function inside. Evaluates a/b = 4/2 = 2 and returns 2 back to point inside function3 where it was called
Now function 3 evaluates 2 - 2 - 4 = -4 and returns -4 back to point inside function 2 where it was called
Now function 2 evaluates -4 * 2 * 4 = -32 and returns -32 back to point inside function 1 where it was called
Now function 1 evaluates -32 + 2 + 4 = -26 and returns -26 back to point where it was called which was inside console.log
*/
让我们采用自下而上的方法(当我们达到返回空数组的基本情况时):-
对于n=0
空数组 []
返回到 countArray
,其中 n
为 1。
所以 for n=1
, [].push(1)
发生了,现在这个数组返回到 countArray
其中 n
是 2 .
所以 for n=2,[1].push(2)
发生了,现在这个数组返回到 countArray
,其中 n
是 3.
这一直持续到 n=5
一切开始,这也是您记录最终结果的地方。
关键是所有这一切都是在从 n=0
回溯到 n=5
而不是从上到下回溯时发生的。
以下是可视化效果(从上到下开始,然后从下到上):-
不幸的是,尽管阅读了我在网上找到的内容,但我仍然不明白它是如何工作的。
这是代码:
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 ]
我理解的最简单的部分是用 n-1 调用函数直到它达到 0 然后将 [] 推到 countArray 常量。
但后来我明白了它是如何将 1 然后 2,然后 3 等作为 n = 0 到 countArray 常量的,然后什么都不应该发生,因为不再调用该函数。
有人可以帮助我吗?
由于您还没有掌握函数调用的窍门,因此在深入研究递归之前,您必须首先完全理解以下程序。我也添加了完整的评论。这里我们从另一个调用一个函数。递归也是一样的,但是同样的函数被自己调用。所以首先要了解以下内容:-
function function1(a,b)
{
let result1 = function2(a,b);
return result1 + a + b;
}
function function2(a,b){
let result2 = function3(a,b);
return result2 * a * b;
}
function function3(a,b){
let result3 = function4(a,b);
return result3 - a - b;
}
function function4(a,b){return a/b}
console.log(function1(4,2))
// It will work like this :-
/*
function1 gets called with a=2,b=4
function2 gets called with a=2,b=4
function3 gets called with a=2,b=4
function4 gets called with a=2,b=4 and doesn't call anymore function inside. Evaluates a/b = 4/2 = 2 and returns 2 back to point inside function3 where it was called
Now function 3 evaluates 2 - 2 - 4 = -4 and returns -4 back to point inside function 2 where it was called
Now function 2 evaluates -4 * 2 * 4 = -32 and returns -32 back to point inside function 1 where it was called
Now function 1 evaluates -32 + 2 + 4 = -26 and returns -26 back to point where it was called which was inside console.log
*/
让我们采用自下而上的方法(当我们达到返回空数组的基本情况时):-
对于n=0
空数组 []
返回到 countArray
,其中 n
为 1。
所以 for n=1
, [].push(1)
发生了,现在这个数组返回到 countArray
其中 n
是 2 .
所以 for n=2,[1].push(2)
发生了,现在这个数组返回到 countArray
,其中 n
是 3.
这一直持续到 n=5
一切开始,这也是您记录最终结果的地方。
关键是所有这一切都是在从 n=0
回溯到 n=5
而不是从上到下回溯时发生的。
以下是可视化效果(从上到下开始,然后从下到上):-