javascript 递归幂函数:如果没有初始返回值,为什么这会起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7657249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recursive power function: Why does this work if there's no initial return value?
提问by u861264
because power(base, exponent) has no return value unless exponent is 0, initially, shouldn't power(base, exponent -1) return 'undefined', and therefore be unmultipliable, initially? So, I am having trouble following the logic of this code. Why/how does it work?
因为 power(base, exponent) 没有返回值,除非 exponent 为 0,最初,power(base, exponent -1) 不应该返回“未定义”,因此最初是不可乘的?因此,我无法遵循此代码的逻辑。为什么/它是如何工作的?
function power(base, exponent) {
if (exponent == 0)
return 1;
else
return base * power(base, exponent - 1);
}
回答by Tim
Look at what happens if you try to calculate 5^3
:
看看如果您尝试计算会发生什么5^3
:
power(5, 3) ... this should give us 125, let's see if it does...
function power(base, exponent) { // base = 5, exponent = 3
if (exponent == 0) // nope, exponent != 0
return 1;
else
return base * power(base, exponent - 1); // return 5 * power(5, 2)
}
... what is power(5, 2)
? ...
...是什么power(5, 2)
?...
function power(base, exponent) { // base = 5, exponent = 2
if (exponent == 0) // nope, exponent != 0
return 1;
else
return base * power(base, exponent - 1); // return 5 * power(5, 1)
}
... what is power(5, 1)
? ...
...是什么power(5, 1)
?...
function power(base, exponent) { // base = 5, exponent = 1
if (exponent == 0) // nope, exponent != 0
return 1;
else
return base * power(base, exponent - 1); // return 5 * power(5, 0)
}
... what is power(5, 0)
? ...
...是什么power(5, 0)
?...
function power(base, exponent) { // base = 5, exponent = 0
if (exponent == 0) // yup, exponent != 0
return 1; // return 1
else
return base * power(base, exponent - 1);
}
... putting that together, in reverse order as we walk back up the stack...
...将它们放在一起,当我们走回堆栈时以相反的顺序......
power(5, 0) = returns 1
power(5, 1) = 5 * power(5, 0) = 5 * 1 = returns 5
power(5, 2) = 5 * power(5, 1) = 5 * 5 = returns 25
power(5, 3) = 5 * power(5, 2) = 5 * 25 = returns 125
... so, power(5, 3) returns 125, as it should.
回答by RobG
It could be more concise:
可以更简洁:
function power(base, exponent) {
return exponent == 0? 1 : base * power(base, --exponent);
}
Howerver an iterative solutionis very much faster:
然而,迭代解决方案要快得多:
function powerNR(base, exp) {
var result = 1;
while(exp--) {
result *= base;
}
return result;
}
回答by Alex Mckay
I think the function makes more sense the other way around, like this:
我认为这个函数反过来更有意义,像这样:
const power = (base, exponent) => {
if (exponent !== 0) {
return base * power(base, exponent - 1);
} else {
return 1;
}
}
The returnof the if statementsare chained together and cannot be resolved until the else statementis executed.
在返回的if语句链接在一起,并直至无法解析else语句执行。
Examples
例子
4^0 = else;
4^0 = 1
4^1 = if * else;
4^1 = 4 * 1;
4^2 = if * if * else;
4^2 = 4 * 4 * 1;
= 4 * 4;
= 16
// Another way of conceptualising it:
4^2 = if(if(else));
= 4(4(1));
= 16;
Rememberit is the returnof the if/else statementsthat is being passed back up the chain to the function that called it.
请记住,它是if/else 语句的返回,被传递回链到调用它的函数。
A slightly stupid metaphor
一个略显愚蠢的比喻
Let's say you want to ask David a question but you don't want to yell, you could ask there person beside you, who could ask the person beside you, who could ask the person beside you, and so on, until the question was asked to David.
假设你想问大卫一个问题但你不想大喊大叫,你可以问你旁边的人,谁可以问你旁边的人,谁可以问你旁边的人,等等,直到问题是问大卫。
const askDavidAQuestion = peopleInBetweenYouAndDavid => {
if (peopleInBetweenYouAndDavid !== 0) {
console.log('I will ask him');
return askDavidAQuestion(peopleInBetweenYouAndDavid - 1);
} else {
console.log('David says no');
}
}
askDavidAQuestion(3);
-> I will ask him
I will ask him
I will ask him
David says no
Only once David's answer is known can that piece of information be passed back up the chain of people that asked the question.
只有知道大卫的答案后,这条信息才能在提出问题的人链中传回。
回答by RyanGonzalezUSA
function pow(base, exponent) {
if (exponent === 0) return 1;
if (exponent > 0) {
return base * pow(base, exponent - 1)
} else {
// handle negative exponent
return 1 / base * pow(base, exponent + 1)
}
}