Skip to content

Instantly share code, notes, and snippets.

@delta48
Last active January 21, 2022 19:29
Show Gist options
  • Select an option

  • Save delta48/4d77e270c3d1d6d0603d56c46d6ee6e3 to your computer and use it in GitHub Desktop.

Select an option

Save delta48/4d77e270c3d1d6d0603d56c46d6ee6e3 to your computer and use it in GitHub Desktop.
// solution for
// https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers
const memo = {};
function fib(n) {
if (n in memo) return memo[n];
if (n <= 2) return 1;
memo[n] = fib(n - 1) + fib(n - 2);
return memo[n];
}
function sumFibs(num) {
let tmp = [];
let nfib = 1;
let i = 1;
while (nfib <= num) {
if (nfib % 2 === 1) tmp.push(nfib);
i++;
nfib = fib(i);
}
return tmp.reduce((a, b) => a + b);
}
sumFibs(4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment