📜  分解一个数字(尾调用优化) - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:13.179000             🧑  作者: Mango

代码示例1
// Factorialize a Number

function factorialize(num) {
    if (num === 0) return 1;
    return num * factorialize(num - 1);
}

factorialize(5);

// OR with Tail Call Optimization (https://stackoverflow.com/questions/33923/what-is-tail-recursion)

function factorialize(num, factorial = 1) {
    if (num == 0) {
        return factorial;
    } else {
        return factorialize(num - 1, factorial * num);
    }
}

factorialize(5);