Last active
August 29, 2015 14:16
-
-
Save lucian303/5efa7adde757dc6089c3 to your computer and use it in GitHub Desktop.
Factorial
This file contains hidden or 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
// Factorial | |
// Reduce | |
var number = 7; | |
arr = []; | |
for (var x = 1; x <= number; x++) { | |
arr.push(x); | |
} | |
alert(arr.reduce(function (a, x) { | |
return a * x; | |
})); | |
// Recursive | |
function fact(x) { | |
if (x === 1) { | |
return 1; | |
} | |
return x * fact(x - 1); | |
} | |
alert(fact(number)); | |
// Loop | |
var factorial = 1; | |
for (var x = 1; x <= number; x++) { | |
factorial *= x; | |
} | |
alert(factorial); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment