Last active
May 18, 2023 13:57
-
-
Save cesarkohl/432f8e831fd41878c780da375e0b574e to your computer and use it in GitHub Desktop.
Fibonacci
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
function fibonacci(len) { | |
const arr = []; | |
//// | |
// Moved the conditionals out of the loop | |
if (len >= 1) { | |
arr.push(0); | |
} | |
if (len >= 2) { | |
arr.push(1); | |
} | |
//// | |
for (let i = 2; i < len; i++) { | |
arr.push(arr[i - 1] + arr[i - 2]); | |
} | |
return arr; | |
} | |
const result = fibonacci(5); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment