Created
March 19, 2020 10:29
-
-
Save Prottoy2938/6c1ad1a6e34f38cad40e288642609099 to your computer and use it in GitHub Desktop.
Generates Fibonacci Sequence and returns it in an Array, base case is 0 and 1.
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
// Get Fibonacci Sequence in an Array, base case is 0 and 1. | |
const fibSequence = (num) => { | |
if(num === 0) return 0; | |
const arr=[0,1]; | |
let counter=2; | |
while(counter <=num){ | |
arr[counter]=arr[counter -1] + arr[counter -2] | |
counter ++ | |
} | |
return arr | |
} | |
fib(5) // returns [0, 1, 1, 2, 3, 5] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment