[英]return function value javascript recursion
using your code with two parameters, assuming it's correct, cheat like this 使用带有两个参数的代码(假设正确),像这样作弊
function f(x) {
function inner(n, i) {
if (n>0) {
return (( n / (1 + i)) + inner(n-1, i+1) );
}
else {
return 0;
}
}
return inner(x);
}
or you can of course do this 或者你当然可以这样做
function test(o) {
if (typeof o == 'number') {
o = { i: 0, n: o };
}
if (o.n > 0) {
return ((o.n / (1 + o.i)) + test({ n: o.n - 1, i: o.i + 1 }));
} else {
return 0;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.