Created
February 15, 2021 18:44
-
-
Save MohammedALREAI/5bb82989d1f205fd2542fbe32c028549 to your computer and use it in GitHub Desktop.
509. Fibonacci Number lettcode
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
function fib(n: number): number { | |
const arr = [1, 1]; | |
let counter = 3; | |
while (counter <= n) { | |
const nextFib = arr[0] + arr[1]; | |
arr[0] = arr[1]; | |
arr[1] = nextFib; | |
counter++; | |
} | |
return n< 1 ? 0: n===1? 1: arr[1] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment