Created
October 12, 2012 00:36
-
-
Save thanklessbastard/3876645 to your computer and use it in GitHub Desktop.
javascript find 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
| // question: | |
| // write an function that will find the factorial of a given number | |
| //Loop [Fastest] ------------------------------- | |
| function factorial (n) { | |
| var i, pN; | |
| if(n === 0) { return } | |
| else { | |
| pN = 1; | |
| for (i = 1; i <= n; i +=1) { | |
| pN *= i; | |
| } | |
| } | |
| return pN; | |
| } | |
| console.log(factorial(10)) | |
| //recursive ------------------------------- | |
| function factorial(n) { | |
| if(n == 0) { return } | |
| } else { | |
| return n * factorial(n - 1); | |
| } | |
| } | |
| console.log(factorial(10)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment