Skip to content

Instantly share code, notes, and snippets.

@eengineergz
Created February 27, 2021 04:52
Show Gist options
  • Save eengineergz/a57bf449f5a8b16eedd1aa9fd71707e2 to your computer and use it in GitHub Desktop.
Save eengineergz/a57bf449f5a8b16eedd1aa9fd71707e2 to your computer and use it in GitHub Desktop.
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