'Fibonacci Numbers - Add odd numbers only - Javascript

So I am trying to develop a formula that will sum all odd Fibonacci numbers up to and including a given number.

For example:

  • Given number is 4. Then result should be 5 (Odd Fibonacci numbers being 1, 1, 3).

Currently this is my code:

function sumFibs(num) {
  var sum = 0;
  for(i=0,j=1,k=0; k<=num;i=j,j=x,k++) {
    x = i + j;
    if (x%2 !== 0) {
      sum +=x;
      if (sum >= sum) {
        break;
      }
    }
  }
  return sum;
}
sumFibs(4);

Clearly the code doesn't work. If I remove the (if sum >= sum) break statement it creates an infinite loop. I have taken the for loop from another post here where a formula was given to create a Fibonacci sequence, but I am having difficulty with what to do after that number is generated, how do I add it. My way of trying to do this is by checking if the modulus is not 0 (which indicates it is an odd number).

Thanks for your help.



Solution 1:[1]

your code is a bit confusing with the variables names and declaration (always try to declare using var). here's a function that gets what you need

function sumFibs(num) {
  var fib0 = 0;
  var fib1 = 1;
  var fib = 1;
  var sum = fib0;
  while ( fib <= num){
    if (fib % 2) {
        sum += fib1;
    }
    fib = fib0 + fib1;
    fib1 += fib0;
    fib0 = fib1 - fib0;
  }

  return sum;
}

Solution 2:[2]

The code is somewhat confused... (what is k for?, the number of elements is irrelevant in the problem definition, also the problem talks about summing fibonacci numbers up to a certain value, not up to when the sum gets past a certain value).

A solution could be

var prev_fib = 0, cur_fib = 1;
var sum = 0;
while(cur_fib <= num) {
    if (cur_fib % 2 !== 0) {
        sum += cur_fib;
    }

    // Move on to next Fibonacci number
    var next_fib = cur_fib + prev_fib;
    prev_fib = cur_fib;
    cur_fib = next_fib;
}

Choosing longer variable names can help

Solution 3:[3]

Smallest way to do this.

http://jsfiddle.net/PuneetChawla/gzr68ccv/

function sum()
{
    var a = 1;
    var b = 0;
    var c = 0;   
    var d = 4;       
    var temp = 1;

    while(c<d-1)
    {
        c = a+b;
        if(c%2 !=0){
        temp = temp+c;
        }
        b=a;
        a=c;
    }
    alert(temp);
}

Solution 4:[4]

I've got a solution close to some others listed, but i find mine to be a bit more readable, so here it is:

function sumOddFibs(num) {
  var sum = 2;

  var prev = 1;
  var curr = 1;
  var next = 2;

  while (next <= num) {
    prev = curr;
    curr = next;
    next = prev + curr;

    if (curr % 2 !== 0) {
      sum += curr;
    }
  }
  return sum;
}

Solution 5:[5]

Here's my solution.

function sumFibs(num) {

  var a = 0, b = 1, f = 1, sum = 0;
  var arr = [0, 1];

  while (f <= num) {
    if (f % 2 == 1)
      sum += f;
    arr.push(f);
    f = a + b;
    a = b; 
    b = f;       
  }

  console.log(arr);
  return sum;
}

Solution 6:[6]

I tweaked Neong's response. It probably isn't as elegant as others but it works. Best

function sumFibs(num) {

   var a = 0, b = 1, f = 1, sum = 0;
   var arr = [0];

   while (f <= num) {
     arr.push(f);
     f = a + b;
     a = b; 
     b = f;       
     }

   var OddOnly = arr.filter(function(value, index, array){
       return value%2 == 1;
   });

   var sumArr = oddOnly.reduce(function(a,b){
       return(a+b);
   })};

   return sumArr;

   }

Solution 7:[7]

function sumFibs(num) {

  if(num === 1)
    return 1;

  var fib = [];
  fib[0] =1;
  fib[1]=1;
  for(var i=2; i<=num;i++){
    fib[i]=fib[i-2]+fib[i-1];
  }

  fib = fib.filter(function(val){
    return (val % 2 !== 0) && (val<=num);
  });

  fib = fib.reduce(function(a,b){
    return a+b;
  });

  return fib;
}

sumFibs(4);

Solution 8:[8]

function sumOddFibonacciNumbers(num) {
    //initialize an array with the first two numbers

      let fib= [1,1]

    //for-loop to push numbers according to the Fibonacci sequence 
    // up to and including the num

      for (let i = 0; i <= num; i++){

        if (fib[i]+fib[i+1] <= num ) {
            fib.push(fib[i] + fib[i+1])
        }
    }
   
    // filter the odd numbers and then reduce to get the sum
    return fib.filter(a => a % 2 !==0).reduce((a,b) => a+b)
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tamiros
Solution 2 6502
Solution 3 Puneet Chawla
Solution 4 Yup.
Solution 5 Neong
Solution 6
Solution 7 Peter Eskandar
Solution 8