Created
May 27, 2019 14:59
-
-
Save bartcis/676d5c742e49dc51163661786a0ed0be to your computer and use it in GitHub Desktop.
Sum up all odd Fibbonaci numbers - solution 2
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
| // 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