一、正向求阶乘
function factorial(n) {
var product = 1, i = 2;
while (i <= n) {
product *= i;
}
return product;
}
alert(factorial(4));
二、逆向求阶乘
function factorial(n) { var product = 1; while (n > 1) { product *= n; n--; } return product; } alert(factorial(4));
三、递归求阶乘
function factorial(n) { if (n <= 1) { return 1; } else { return n * factorial(n - 1); } } alert(factorial(4));