简体   繁体   English

返回函数值javascript递归

[英]return function value javascript recursion

Using recursion return value of this function. 使用此函数的递归返回值。 Only one parameter is allowed in function. 功能中仅允许使用一个参数。 Also global variables outside of function are not allowed. 同样,函数之外的全局变量也是不允许的。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM