Created
January 7, 2014 16:17
-
-
Save dtothefp/8301762 to your computer and use it in GitHub Desktop.
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
| // Loop Version | |
| function loopFib(n){ | |
| var first = 0; | |
| var second = 1; | |
| var sum; | |
| for(var i = 2; i <= n; i++){ | |
| sum = first + second; | |
| first = second; | |
| second = sum; | |
| } | |
| return sum; | |
| } | |
| // Recursive Version | |
| function recFib(n){ | |
| if (n === 0){ | |
| return 0; | |
| } | |
| else if (n === 1){ | |
| return 1; | |
| } | |
| else { | |
| return recFib(n-1) + recFib(n-2); | |
| } | |
| } | |
| console.log(loopFib(5)); | |
| console.log(recFib(5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment