Last active
November 28, 2016 08:05
-
-
Save Ajax30/15f4cb27fca91cea4be0182ba9d799ef to your computer and use it in GitHub Desktop.
Calculate the factorial of a number passed as the single parameter of a function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fact() { | |
arr = []; | |
var n = arguments[0]; | |
if (n == 0) { | |
arr.push(1); | |
} else { | |
for (i = 1; i <= n; i++) { | |
arr.push(i); | |
} | |
} | |
var result = arr.reduce(function(a, b) { | |
return a * b; | |
}); | |
console.log(result); | |
} | |
fact(1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment