Skip to content

Instantly share code, notes, and snippets.

@memon07
Last active May 8, 2019 09:56
Show Gist options
  • Save memon07/c97a3aec2ceb0e3f2a03e6f835d1963b to your computer and use it in GitHub Desktop.
Save memon07/c97a3aec2ceb0e3f2a03e6f835d1963b to your computer and use it in GitHub Desktop.
```
Method 1
```
let fibonacci = function(n){
if(n == 1) {
return [0,1]
}
else {
let s = fibonacci(n-1)
console.log(s)
s.push(s[s.length -1] + s[s.length -2])
return s
}
}
console.log(fibonacci(8))
```
Method 2
```
function fibonacci(n) {
const fibSequence = [1];
let currentValue = 1;
let previousValue = 0;
if (n === 1) {
return fibSequence;
}
let iterationsCounter = n - 1;
while (iterationsCounter) {
currentValue += previousValue;
previousValue = currentValue - previousValue;
fibSequence.push(currentValue);
iterationsCounter -= 1;
}
return fibSequence;
}
console.log(fibonacci(9))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment