``````function factorial(n) {
if (n === 0) {
return 1;
}
return n * factorial(n - 1);
}
``````

• factorial(6)
• 6 * factorial(5)
• 5 * factorial (4)
• 4 * factorial(3)
• 3 * factorial(2)
• 2 * factorial(1)
• 1 * factorial(0)
• 1
• （恢復先前的執行） 1 * 1 = 1
• （恢復…） 2 * 1 = 2
• (…) 3 * 2 = 6
• … 4 * 6 = 24
• 5 * 24 = 120
• 6 * 120 = 720
• factorial(6) = 120

``````function factorial(n, res) {
if (n === 0) {
return res;
}
return factorial(n - 1, res * n);
}
``````

``````function factorial(n) {
function inner_factorial(n, res) {
if (n === 0) {
return res;
}
return inner_factorial(n - 1, res * n);
}
return inner_factorial(n, 1);
}
``````

• factorial(6)
• 內部匿名 function（iaf）和（n = 6, res = 1）被呼叫
• iaf(5, 1 * 6)
• iaf(4, 6 * 5)
• iaf(3, 30 * 4)
• iaf(2, 120 * 3)
• iaf(1, 360 * 2)
• iaf(0, 720)
• 720
• 720
• 720
• 720
• 720
• 720
• 720
• iaf (6, 1) = 720
• factorial(6) = 720

• factorial(6)
• iaf(6, 1)
• iaf(5, 6)
• iaf(4, 30)
• iaf(3, 120)
• iaf(2, 360)
• iaf(1, 720)
• iaf(0, 720)
• 720

``````res = 1;
n = 6;

while(n > 1) {
res = res * n;
n--;
}
``````

>_ Improve this tip