Created
February 27, 2021 04:52
-
-
Save eengineergz/a57bf449f5a8b16eedd1aa9fd71707e2 to your computer and use it in GitHub Desktop.
This file contains 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) { | |
let mostRecentCalcs = [0, 1]; | |
if (n === 0) return mostRecentCalcs[0]; | |
for (let i = 2; i <= n; i++) { | |
const [secondLast, last] = mostRecentCalcs; | |
mostRecentCalcs = [last, secondLast + last]; | |
} | |
return mostRecentCalcs[1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment