給出num,實現num的階乘javascript
factorialize(5) should return 120
遞歸函數java
條件語句中止遞歸less
代碼1:函數
function factorialize(num) { if(num === 0){ return 1; } return num * factorialize(num-1); } factorialize(5);
代碼2(樓下道友的尾遞歸代碼):code
function factorial(n, total = 1) { if (n === 1) return total; return factorial(n - 1, n * total); } factorial(5) // 120
有其餘好的方法或思路的道友,不妨在沙發區神交一番。ip