Last active
May 8, 2019 09:56
-
-
Save memon07/c97a3aec2ceb0e3f2a03e6f835d1963b to your computer and use it in GitHub Desktop.
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
``` | |
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