freeCodeCamp Challenge Guide: Sum All Odd Fibonacci Numbers

I am curious whether it would be possible to solve this challenge recursively

1 Like

meh.


function sumFibs(num) {
  var a = 0, b = 1, c, fibArr = [0, 1];
  while(a + b <= num) {
    c = b;
    b = a + b;
    a = c;
    fibArr.push(b);
  }
  num = fibArr.filter(function(d) {return d > 0 && d % 2 > 0 }).reduce(function(e, f) {return e + f}, 0);
  
  return num;
}

The input ‘b’ is the number in the fibonacci sequence. Start calling the recursive function with 0 and 1, to get 1,1,2…

Otherwise counter and while loop are the same as basic solution.

function sumFibs(num) {
  
  var oddsum =0;
  
  function fib(a,b){
while (b<=num){
  if (b%2===1){
    oddsum+=b;
  }
  return fib(b,a+b);
}   
  } 
  fib(0,1);

  return oddsum;
}

sumFibs(1);
1 Like
function sumFibs(num) {
  var first = 1, second = 1, next, arr = [];
  while (first <= num) {
    arr.push(first);   
    next = first + second;
    first = second;
    second = next;
  } 
  return arr.filter(function(val) {
    return val % 2 !== 0;
  }).reduce(function(acc, curr) {
    return acc + curr;
  });
}

sumFibs(10);
1 Like
function sumFibs(num) {
  var fib = [0, 1];
  var i = 2;
  while (fib[i-1] <= num) {
    fib[i] = fib[i-1] + fib[i-2];
    i++;   
  }
  fib.pop();
  fib = fib.filter(function(item) {
    return item % 2 !== 0;
  }); 
  return fib.reduce(function(a, b) {
    return a+b;
  });

}
function sumFibs(num) {
    var fib = [1,1];
	var i = 2;
	var count = 2;

	while ((fib[i-1] + fib[i-2]) <= num){
		fib[i] = fib[i-1]+fib[i-2];
		if(fib[i]%2 !== 0) {
			count += fib[i];
		}
		i++;
	}
	
	return count;
}
1 Like
function sumFibs(num) {  
var fibs = [1],
  i  = 1;      
while(i<=num){   
fibs.push(i);
i= fibs[fibs.length-2]+fibs[fibs.length-1];
}
return fibs.filter(function(a){return a%2!==0;}).reduce(function(a,b){return a+b;});
}
sumFibs(1000);

var memo = { 0:1, 1:1 };

function fib(num){
** memo[num] = memo[num] ? memo[num] : fib(num -1) + fib(num-2); **
** return memo[num]; **
}

function sumFibs(num) {
** var i=0, sum=0;**
** **
while(fib(i) <= num){_
_
sum += fib(i)%2 !== 0 ? fib(i) : 0;_
_
i++;_
_
}_
_
return(sum);_
_
}**_

function sumFibs(num) {
  var lastTwo = [1,1];
  var runningTotalOfOdd = 0;
  while (lastTwo[0] <= num)
    {
      if (lastTwo[0] % 2 === 1)
        runningTotalOfOdd += lastTwo[0];
      
      if (lastTwo[1] < num && lastTwo[1] % 2 === 1)
        runningTotalOfOdd += lastTwo[1];
      lastTwo = incrementOdd(lastTwo);
    }
  return runningTotalOfOdd;
}

function incrementOdd(arr)
{
  var temp = [];
  temp.push(arr[0] + arr[1]);
  temp.push(temp[0] + arr[1]);
  return temp;
}

I have written this and not seen someone else do it. It’s very basic and uses for-loops rather than a while-loop, but it does the job and you can control every step. My array only used the first 50 Fibonacci numbers, which means that if num is bigger than the 50th number, it won’t work, but seeing as these numbers get big really fast, this was enough to pass the test. Should I rewrite it so that it will work for bigger numbers as well?

function sumFibs(num) {

var arr = [];
arr[0] = 1;
arr[1] = 1;

for (var i = 2; i <= 50; i++){
arr[i] = arr[i-1] + arr[i-2];
}

for(var j = 0; j < arr.length; j++){
if (arr[j]%2 === 0){
arr.splice(j, 1);
}
}

var sum = 0;

for (var k = 0; k < arr.length; k++){
if(arr[k] > num){
arr.splice(k);
}
}

for (var l = 0; l < arr.length; l++){
sum += arr[l];
}

return sum;
}

sumFibs(4);

1 Like

This was a bit challenging for me!

function sumFibs(num) {
  var xt,
    pt = 2;
  var fb = [1, 1];

  for (var k = 2; k <= num; k = xt) {
    xt = fb[fb.length - 1] + fb[fb.length - 2];
    if (xt <= num) {
      fb.push(xt);
      if (xt % 2 !== 0) {
        pt += xt;
      }
    }
  }
  return pt;
}

sumFibs(10);
1 Like

I don’t know what is the problem with my code. My browser freezes when I run it like it was stuck in an infinite loop but I don’t see any clues of that in the portion of code that I wrote:

I had some trouble figuring it without the use of arrays since it’s a common mistake getting stuck in infinite loops. Here is my code:

I’ve had that problem as well. The problem is that num can be a very high number, in which case there will be many many iterations, which is probably why the browser gets stuck. It just takes a very long time to for example do 1000 iterations. Use a while loop or use a number instead of a variable, and then make that number not too high (that is what I did: in a Fibonacci-sequence, the 50th number is already very high, so I used 50).

You can also try this, pretty basic one:

function sumFibs(num) {
  var fabo = [];
  var sum = 1;
  fabo[0] = 1;
  fabo[1] = 1;
  for(i=1; fabo[i] <= num; i++) {
    fabo[i+1] = fabo[i] + fabo[i-1];
    if(fabo[i] % 2 !== 0) {
      sum += fabo[i];
    }
  }
  return sum;
}

2 Likes

Here is my solution:

function sumFibs(num) {
var arr = [1,1,];
var sum = 2; // cuse I start from the third number
for (var i=2;i<=num;i++) {
arr[i] = arr[i-1] + arr[i-2];
if (arr[i] % 2 !== 0 && arr[i] <=num) {sum += arr[i];}
 }
return sum;
}
3 Likes

Here is my code. I’m a bit too obsessed with for loops. Starter_1 and starter_2 are the 1 and 1 that the fibonacci sequence starts with. I then replace them as I go through the fibonacci sequence, store the calculated fibonacci number in an array, and exit the fibonacci sequence once I reach the number given. Then, I iterate through the fibonacci elements in the array and add together only the odd numbers.

<code>

function sumFibs(num) {
  var starter_1 = 1;
  var starter_2 = 1;
  var fibonacci = 0;
  var arr = [starter_1, starter_2];
  var summer = 0;
  
  for (var i = 0; i < num; i++) {
    fibonacci = starter_1 + starter_2;
    starter_2 = starter_1;
    starter_1 = fibonacci;
    if (fibonacci > num) {
      break;
    }
    else {
       arr.push(fibonacci);
    }
  }
  
  for (var j = 0; j < arr.length; j++) {
    if (arr[j] % 2 != 0) {
      summer += arr[j];
    }
  }
  
  return summer;
}

sumFibs(4);

</code>

Can anyone explain why you need the “else return prev” and what it does, like I know it doesn’t work without it but what are we returning , I see we are searching with if statement for odd number and then if it is we add previous and current together but then if it isn’t odd we return the previous … to where? To the array ? Doesn’t that then give us two of the same? Probably me just being a bit dumb but I hate not understanding what happen to stuff

Yes, I had to disable the infinite loop protection putting //noprotect at the first line of the code. It took a time to evaluate, but it worked. However, is not a good practice to do that because in the most cases you have an infinite loop without you notice.

1 Like
function sumFibs(num) {
  
  var fibonacci = [1, 1];
  
  for (var i = 0; i <= num; i++) {
    // calculate fibonacci number
    var fibNum = fibonacci[i] + fibonacci[i+1];
    // fibonacci number must be <= num
    if (fibNum <= num) {
      // push fibonacci number in array
      fibonacci.push(fibNum);
    } else {
      // stop the loop
      break;
    }
  }
  
  return fibonacci.filter(function(e) {
    // filter only odd numbers
    return e % 2 !== 0;
  }).reduce(function(a, b) {
    // sum all odd numbers
    return a + b;
  });
  
}
2 Likes