这个递归函数是如何得到这个输出的
How does this recursive function come to this output?
有人能解释一下这个递归函数的输出吗?谢谢你!
function test(a) {
while (a > 0) {
a -= 1;
test(a);
console.log('after first invocation: ' + a);
}
}
test(3);
输出:after first invocation: 0
after first invocation: 1
after first invocation: 0
after first invocation: 2
after first invocation: 0
after first invocation: 1
after first invocation: 0
代码100%按照你的要求去做!
loop 1 :
value of a is 3, is it bigger then 0? Yes!
3 - 1 = 2 (why not a-- ...)
call function test(2)
is 2 bigger the 0? Yes!
2 - 1 = 1
call function test(1)
is 1 bigger the 0? Yes!
1 - 1 = 0
call function test(0)
is 0 bigger then 0 ? NO!
console.log(('after first invocation: ' + 0)
我不认为我必须对每个输出都这样做,但是我想你明白了吗?
相关文章:
- 递归函数中断
- 将jQuery对象传递到setTimeout递归函数中
- 对象与递归函数的比较
- 循环内部的递归函数未按预期工作
- 递归函数返回不正确
- 递归函数编程困境
- 给定一个带有数字的数组,我如何编写一个递归函数,当 2 个元素加起来为一个目标时,它会在数组中查找索引
- 返回不会退出 javascript 中的递归函数
- jquery递归函数转换为非递归函数
- AngularJS,promise带有递归函数
- 如何停止此递归函数
- 如何将下面的递归函数转换为纯函数
- jQuery setTimeout ajax递归函数在即时消息程序中短时间后抛出错误
- Javascript递归函数引用了这一点
- 如何将这个递归函数转换为迭代函数
- jQuery递归函数调用和Javascript之间有区别吗;s setInterval
- 如何在 javascript 中获取递归函数来输出每个返回值
- JavaScript递归函数未返回正确的输出
- 这个递归函数是如何解析的,为什么它输出它所做的
- 这个递归函数是如何得到这个输出的