Skip to content

Instantly share code, notes, and snippets.

@bartcis
Created May 27, 2019 14:59
Show Gist options
  • Select an option

  • Save bartcis/676d5c742e49dc51163661786a0ed0be to your computer and use it in GitHub Desktop.

Select an option

Save bartcis/676d5c742e49dc51163661786a0ed0be to your computer and use it in GitHub Desktop.
Sum up all odd Fibbonaci numbers - solution 2
// All odd Fibbonaci's
function sumOddFibsAdv(number) {
// 1. Create array with the two first values from the sequence
let fibs = [0, 1];
// 2. Create the number sequence till defined parameter
for (let fib = 1; fib <= number; ) {
fibs.push(fib);
fib = fibs[fibs.length - 1] + fibs[fibs.length - 2];
}
// 3. Sum up odd values using JS reduce methods
return fibs.reduce((previous, current) => {
return current % 2 ? previous + current : previous;
});
}
console.time('Start Algo 2');
console.log(sumOddFibsAdv(467));
console.timeEnd('Start Algo 2'); // Start Algo 2: 0.35498046875ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment