function factorial(a) { var x = 1; for (var i = 2; i <= a; i++) x *= i; return x; } alert( factorial(5) );