Skip to content

Instantly share code, notes, and snippets.

@cesarkohl
Last active May 18, 2023 13:57
Show Gist options
  • Save cesarkohl/432f8e831fd41878c780da375e0b574e to your computer and use it in GitHub Desktop.
Save cesarkohl/432f8e831fd41878c780da375e0b574e to your computer and use it in GitHub Desktop.
Fibonacci
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