Last active
August 15, 2024 10:50
-
-
Save casweeney/7ffa19de775fe8661dd405f4d9ae4499 to your computer and use it in GitHub Desktop.
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
// Iteration | |
function fibonacci(n) { | |
const fib = [0, 1]; | |
for (let i = 2; i <= n; i++) { | |
fib[i] = fib[i - 1] + fib[i - 2]; | |
} | |
// const fibb = { | |
// fibonacci: fib, | |
// fibonacciLength: fib.length, | |
// number: fib[n], | |
// }; | |
return fib[n]; | |
} | |
// Recursion | |
function fibonacci(n) { | |
if (n < 2) { | |
return n; | |
} | |
return fibonacci(n - 1) + fibonacci(n - 2); | |
} | |
console.log(fibonacci(2)); // [0, 1] | |
console.log(fibonacci(3)); // [0, 1, 1] | |
console.log(fibonacci(7)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment