showing results for - "sum all odd fibonacci numbers"
Carl
31 Jan 2017
1function sumFibs(num) {
2  var prevNumber = 0;
3  var currNumber = 1;
4  var result = 0;
5  while (currNumber <= num) {
6    if (currNumber % 2 !== 0) {
7      result += currNumber;
8    }
9
10    currNumber += prevNumber;
11    prevNumber = currNumber - prevNumber;
12  }
13
14  return result;
15}
16
17// test here
18sumFibs(4);
19
Humberto
06 Jan 2020
1const sumFibs = num => {
2  // Fibonacci always starts with a [1,1] so, we just initialize it
3  const fibNum = [1,1] // Get all the fibonacci numbers for that range
4  const fibOdd = [1,1] // Get all the odd fibonacci numbers for that range
5  
6  // Loop for through for n times(num)
7  for(let i = 0; i < num; i++){
8    // Add the current index to the next index to get the later fib number
9    const fibAdd = fibNum[i] + fibNum[fibNum.length - 1]
10
11    if(fibAdd > num) break; // Stop pushing or looping if added indexes is more than number(num)
12    fibNum.push(fibAdd) // Push to all fibNum if added indexes is less than the number(num)
13
14    // Push odd fib numbers to fibOdd, so we can now uniquely add only odd numbers
15    if(fibAdd % 2 !== 0) fibOdd.push(fibAdd)
16  }
17
18  // Return reduced or added fibonacci odd numberss
19  return fibOdd.reduce((acc,cur) => acc + cur)
20}
21
22sumFibs(75024);
23
24// With love @kouqhar
queries leading to this page
in a fibonacci sequence find the odd and even numbersfibonacci series odd sumsum of range of odd numbers calculatorformula to sum even fibonacci numberssum of fibonacci numbersadd even fibonacci numberssum of fibonacci numbers formulahow to find odd fibonacci numbers javascriptsum odd numbers lower fibonacciwhat is the sum of all odd numbers in the fibonacci sequence that are less than 10 2c000 3fcompute the sum of odd numbers from 1 to nsum of even fibonacci numbersfind odd number in a fibonacci sequencefinding common factor for odd numbers2 odd numbers always evensum of even numbers in fibonacisum of odd fibonaccifind the sum of the first three odd terms of the fibonacci sequence sum of all odd numbers in the fibonacci sequence that are lessreturn the sum of all odd fibonacci numbers that are less than or equal to numreturn all odd fibonacci numberseven fibonacci numberssum of all odd fibonacci numbers that are less than or equal to 60num 60 sum of all odd numbers in fibonacci sequence under 10000odd numbers in fibonaccifind three consecutive odd numbers whose sum is 147 sum of the odd terms of the fibonacci sequencesum of odd fibonacci numbersfibonnacci sum of even numberssum of n odd numberssum all odd fibonacci numbersdifference between odd and even numbers fibonacci series sum of even numberfind total number of odd and even fibonacci numbers between two numberssum odd numbersfibonacci sequence odd numberssegregation of odd and even numberseven fibonacci numbers sumsum up all odd numbers in fibonacci sequence that are lesswhat is the sum of all odd numbers in the fibonacci sequence that are less than 10 2c000sum of all even fibonacci numbers under 4 millionsum all odd fibonacci numbers