[英]How recursive function is returning value?
function reverse(str){
if(str.length <= 1) {
return str;
}
return reverse(str.slice(1)) + str[0];
}
console.log(reverse("two"))
When the last return str
line hits inside the if
statement, it returns 'o'
.当最后一个
return str
行在if
语句内命中时,它返回'o'
。 Yet there is an extra + str[0]
outside the if
statement.然而,在
if
语句之外还有一个额外的+ str[0]
。 Shouldn't it be returning 'oo'
rather than 'o'
?它不应该返回
'oo'
而不是'o'
吗?
The function is working completely fine, but when I try to visualize it I get confused. function 工作正常,但是当我尝试将其可视化时,我感到困惑。
It will enter the if
branch only when the string length is less than or equal to 1只有当字符串长度小于等于1时才会进入
if
分支
eg.例如。 str = "o"
str = "o"
then it will enter然后它会进入
if(str.length <= 1) {
return str; //here your function will return the answer to main() and will exit this function
}
OUTPUT OUTPUT
o
Since you are writing return str
it will immediately return from that function and will not execute any following lines of code.由于您正在编写
return str
,它将立即从该 function 返回,并且不会执行任何以下代码行。 So it will not execute this:所以它不会执行这个:
return reverse(str.slice(1)) + str[0]; //this part will never be reached
The above part will be executed only if the str len>=2
仅当
str len>=2
时才会执行上述部分
Let us now understand the recursion stack现在让我们了解递归堆栈
str = "two"
This is how recursion works--这就是递归的工作原理——
reverse(two) = reverse(wo)+ t = reverse(o)+ wt = o+wt = owt
Note: reverse(o)
will simply return o
as length of string is 1注意:
reverse(o)
将简单地返回o
因为字符串的长度是 1
This is how it happens in order of the function's internal behavior:这是按照函数内部行为的顺序发生的:
reverse('two')
is calledreverse('two')
reverse('two')
calls reverse(str.slice(1))
which evaluates to a call to reverse('wo')
reverse('two')
调用reverse(str.slice(1))
计算结果为调用reverse('wo')
reverse('wo')
calls reverse(str.slice(1))
which evaluates to a call to reverse('o')
reverse('wo')
调用reverse(str.slice(1))
计算结果为调用reverse('o')
if(str.length <= 1)
evaluates to true and the the reverse('o')
call returns 'o'
to the caller if(str.length <= 1)
计算结果为true ,并且reverse('o')
调用返回'o'
给调用者reverse('wo')
call returns 'ow'
to the caller ( str[0]
was 'w'
) reverse('wo')
调用返回'ow'
给调用者( str[0]
是'w'
)reverse('two')
call returns 'owt'
to the caller ( str[0]
was 't'
) reverse('two')
调用返回'owt'
给调用者( str[0]
是't'
) and the final result is 'owt'
, as expected.正如预期的那样,最终结果是
'owt'
。
So in所以在
return reverse(str.slice(1)) + str[0];
str[0]
will not be added to reverse(str.slice(1))
until the function call returns.在 function 调用返回之前,不会将
str[0]
添加到reverse(str.slice(1))
中。 and when it returns the value of str[0]
will be the value it was when reverse(str.slice(1))
was called.当它返回时
str[0]
的值将是调用reverse(str.slice(1))
时的值。
When the if
statement is satisfied and evaluates to true it exits the current function call immediately returning the value 'o'
to the caller, since there is a return statement inside the if .当
if
语句满足并计算为true时,它退出当前的 function 调用,立即将值'o'
返回给调用者,因为if中有一个 return 语句。
if(str.length <= 1) {
return str; // <-- exits here and returns 'o'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.