Last active
June 5, 2017 14:45
-
-
Save jukben/609439dc85244e1d761ed85f6f208119 to your computer and use it in GitHub Desktop.
Fib
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
// using iteration | |
const fib = (number) => { | |
const results = [0, 1] | |
for (let i = 0; i <= number - 2; i++){ | |
results[i+2] = results[i+1] + results[i]; | |
} | |
return results[number]; | |
} | |
// using recursion (be aware of stack overflow :neckbeard:) | |
const fib = (number) => { | |
if (number<2) return number | |
return fib(number - 2) + fib(number - 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment