📜  分解一个数字 - 任何代码示例

📅  最后修改于: 2022-03-11 14:59:43.384000             🧑  作者: Mango

代码示例2
// 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);