了解返回反向计数的递归循环 [英] Understanding recursive loop that returns an inverted count
问题描述
inner conditional: 5
inner conditional: 4
inner conditional: 3
inner conditional: 2
inner conditional: 1
0
创建上述输出的函数如下,我完全理解它是如何工作的.
The function that created the above output is below and I perfectly understand how it works.
function loop(counter){
if(counter > 0){
console.log("inner conditional: " + counter );
return loop(counter - 1)
}
console.log(counter);
return counter
}
loop(5)
<小时>
当修改函数使得内部条件不返回函数时,console.log 会显示以下内容.
When the function is modified so that the inner condition does not return the function the console.log displays the following.
inner conditional: 5
inner conditional: 4
inner conditional: 3
inner conditional: 2
inner conditional: 1
0
1
2
3
4
5
下面是负责上述输出的函数.
Below is the function that is responsible for the above output.
function loop(counter){
if(counter > 0){
console.log("inner conditional: " + counter );
loop(counter - 1)
}
console.log(counter);
return counter
}
loop(5)
我完全不明白这一点.我只想解释一下创建扩展前向计数发生了什么.
I do not understand this at all. I would simply like an explanation as to what is happening to create the extended forward count.
我使用以下工具来可视化逐步执行代码,但我仍然没有理解"它:http://pythontutor.com/javascript.html#mode=display
I used the following tool to visualize stepping through the code and I still don't "get" it: http://pythontutor.com/javascript.html#mode=display
推荐答案
我一直在寻找的答案是 1-5 计数正在从堆栈顶部拉出.
The answer that I was looking for is that the 1-5 count is being pulled off the top of the stack.
我发现了一个很棒的 YouTube 视频,它在此处通过可视化解释了这一点:
I found a great youtube video that explains this here with visualizations:
https://www.youtube.com/watch?v=s8JpA5MjYac
这篇关于了解返回反向计数的递归循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!