JS中的递归计数达到0后会发生什么? [英] Recursive count in JS what happen after reaching 0?

查看:80
本文介绍了JS中的递归计数达到0后会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,尽管阅读了我在网上发现的内容,但我仍然不明白它是如何工作的.

Unfortunately despite reading what I have found on line I still do not understand how it works.

这是代码:

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常量.

The easy part I understand is the one calling the function with n-1 until it reach 0 and then it push [] to the countArray const.

但是我确实了解到它如何对n = 0的countArray const分别具有1、2、3等,然后什么也不会发生,因为不再调用该函数.

But then I do understand how it had 1 then 2, then 3 etc. to the countArray const as n = 0 and then nothing should happend because the function is not called anymore.

有人可以帮助我吗?

推荐答案

由于您不熟悉函数调用,因此在进入递归之前,您必须首先完全理解以下程序.我还添加了完整的评论.在这里,我们从另一个调用一个函数.递归也是相同的,但是相同的函数被自身调用.因此,首先了解以下内容:-

As you are not getting the hang of function calls, before diving into recursion, you have to first absolutely understand the following program. I have added comments with full flow as well. Here we are calling one function from another. Recursion is also same but the same function getting called by itself. So first understand the below :-

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

*/

让我们采取自下而上的方法(当我们到达返回空数组的基本情况时):-

Let's go bottom-up approach (when we reach base case of returning empty array) :-

用于 n = 0

for n=0

空数组 [] 返回到 n 为1的 countArray .

因此对于 n = 1 ,发生 [].push(1),现在此数组返回到 countArray,其中 n 为2.

So for n=1, [].push(1) happens and now this array is returned to the countArray where n was 2.

因此对于n = 2 ,发生 [1] .push(2),现在此数组返回到 countArray ,其中 n 为3.

So for n=2, [1].push(2) happens and now this array is returned to the countArray where n was 3.

这一直持续到 n = 5 为止,这也是您记录最终结果的地方.

This goes on till n=5 when it all started and also this is where you are logging the final result.

问题的关键在于,这一切都是在从 n = 0 追溯到 n = 5 而不是从上到下的过程中发生的.

The crux is that all this happend while backtracking from n=0 to n=5 and not from top to down.

以下是可视化效果(从上到下,然后从下到上开始):-

Following is the visualization (Starts with going top to bottom and then bottom to top) :-

这篇关于JS中的递归计数达到0后会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆