freeCodeCamp Challenge Guide: Sum All Odd Fibonacci Numbers

I wrote a recursive version a while ago:

const sumFibs = (n,prev=0,curr=1,sum=0) => curr > n ? sum : sumFibs(n, curr, prev+curr, sum+(curr%2&&curr));
4 Likes
function sumFibs(num) {
  var arr = [1,1];
  var i = 0;
  var curr = 0;
  var sum = 0;
  while(i <= num){
    curr = arr.slice(-2,-1)[0] + arr.slice(-1)[0];
    i = curr + arr.slice(-1)[0];
    arr.push(curr);
  }
  for(var j = 0; j < arr.length; j++){
    if(arr[j] % 2 == 1){
      sum += arr[j];
    }
  }
  return sum;
}

Here’s another solution for the intermediate way.

I think it should be ‘if it is ODD, add it to the result.’

Please correct me if I’m wrong?

Still new to coding, it’s my 11th day here :slight_smile:

*PS: Just saw that @curiousnoob mentioned this on Mar 27.

Two variables, two loops, one condition.

Some of the solutions on here hurt my head, of course your own code is always the easiest to read and understand probably.

What do you guys think? Easy to read and understand? Or does it give you a headache?

Basic solution I think.
Critiques welcome.

function sumFibs(num) {
  var fib = [0,1];
  var oddSum = 0;

  // Creates fib array up to one fibonacci number after num
  while (fib[fib.length - 1] < num) {
    fib.push(fib[fib.length - 1] + fib[fib.length - 2]);
  }
  
  // Adds odd numbers in array less than num and num if odd
  for (var i = 0; i < fib.length; i++) {
    if (fib[i] % 2 === 1 && fib[i] <= num) {
      oddSum += fib[i];
    }
  }
  
  return oddSum;
}

https://repl.it/JXeF/8

I can’t remember the last time I used a do-while loop, but it worked well for me here.

I do the sum first, and in order to only sum the odd values, I use the x * ( x % 2 ) technique, which returns a value of 0 when x is even, and x when x is odd.

After that, I move up the Fibonacci sequence and then continue the loop while the highest number in the sequence (f3) is less than or equal to num. Once it’s greater than num, I return the sum.

function sumFibs(num) {
  var sum = 0, f1 = 0, f2 = 1, f3;
  
  do {
    sum += f2 * (f2 % 2);
    f3 = f1 + f2;
    f1 = f2;
    f2 = f3;
  } while (f3 <= num);

  return sum;
}
1 Like
function sumFibs(num) {
  
  var arr = [1, 1];
  
  for (var i = 2; i <= num; i++) {
    var fibNum = arr[i-1] + arr[i-2];
    if (fibNum <= num) {
      arr.push(fibNum);
    } else {
      break;
    }
  }
  
  return arr.filter(function(a){
    return a<=num && a%2!=0;
  }).reduce(function(sum, value) {
    return sum + value;
  }, 0);
}

sumFibs(75025);
function sumFibs(num) {
    var fibSum;
    var prevNo = 0;  
    var newNo = 1;    
    var fibArr = [];  
    fibArr.push(1);  
  
    for (var i = 1; i <= num; i++) {
        fibSum = newNo + prevNo; 
        if((fibSum%2)!=0&&fibSum<=num)
        {
         fibArr.push(fibSum);
        }
        prevNo = newNo;  
        newNo = fibSum;  
    }
    return fibArr.reduce(function (a, b) { return a + b; });
}
sumFibs(75025);

my solution in a more declarative way

function sumFibs(num) {
  // fibonacci function
  function fib(n) {
    let [a, b] = [1, 1], // using ES6 Array destructuring assignment
         res = [];  
    while (a <= n) {
      res.push(a);
      [a, b] = [b, a+b];
    }
    return res;
  }
  // checks for odd numbers
  let isOdd = n => n % 2 === 1
  // filter odd numbers and sum them
  return fib(num).filter(isOdd).reduce((s, v) => s+v);
}

sumFibs(4);

Hello guys,

Great solutions in here! Congrats! I’ve had fun with this one! Here’s mine:

function sumFibs(num) {
  
  var result = 0,
      oddFibs = [];
  
  for(var allFibs = [1], x = 0, y = 1, z = 0, fN; z <= num - y; x = y, y = fN, z++){
    fN = x + y;
    if(fN <= num)    
    allFibs.push(fN);
  }
  for(var i = 0; i < allFibs.length; i++){
    if(allFibs[i] % 2 !== 0){
      oddFibs.push(allFibs[i]);
    }
  }
  result = oddFibs.reduce(function(a,b){
    return a + b;
  });
  return result;
}
//You're outta sight!
sumFibs(4000000);
function sumFibs(num) {
var i=1;
var j=1;
var sum=0;
while (i<=num){
  if (j%2!==0 && j<=num){
    sum+=j;
  }
  if (i%2!==0 && i<=num){
    sum+=i;
  }
i+=j;
j+=i;

}
return sum;
}

Very interesting challenge! I’d like to know why my code doesn’t work, any ideas? I know how to do it in a different way, but I’d like to understand my mistake so I can avoid it in future. Thank you :tulip:

function sumFibs(num) {
var i;
var fib = [];

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

function isOdd(value) {
return value % 2 === 1;
}

var result = 0;
var fibOdd = fib.filter(isOdd);

 for(i=0; i<fibOdd.length; i++){
 result += fibOdd[i];
  }

    return result;
}

sumFibs(20);

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

I was blocked trying to do it with an array (only god knows why)… of course later i checked there’s no need to use arrays here, but anyway… i solved both ways, but left my solution with the array form, as it was the original one in my case. There goes:

  function sumFibs(num) {
  var arr=[];
  arr[0]= 1;
  arr[1] = 1;
  var odd= 2;
  var i=1;
  
  while (arr[i]+arr[i-1] <= num){
    arr.push(arr[i] + arr[i-1]);
    if(arr[i+1] % 2 !==0 ){
      odd+= arr[i+1];
    }
    
    i++;
  }
  
  
  return odd;
}

sumFibs(1000);

Hey there, i’m far from being an expert…just learning yet, but i took my time to go through your code. Here’s my solution using your example:

function sumFibs(num) {
var i;
var fib = [];

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

function isOdd(value) {
  return value % 2 !==0;
}

var result = 0;
var fibOdd = fib.filter(isOdd);

 for(i=0; i<fibOdd.length; i++){
 result += fibOdd[i];
  }

    return result;
}

and here’s some thoughts about your code

fib[0] = 0; // first number in fib sequence is 1
fib[1] = 1;
for(i=2; i<=num; i++){ //i is your index, what you should check is if the sum of fib numers are <= num
fib[i] = fib[i-2] + fib[i-1]; //you can use fib.push, but your index should be at 1 for that
}

function isOdd(value) {
return value % 2 === 1;// odd numbers cand have others remainders, not only "1". Use instead value % 2 !==0
}
2 Likes

@gpereiraPy, works like a charm, thanks a lot! Also it makes a lot of sense with your description. Though my way using

function isOdd(value) {
return value % 2 === 1;
}

works too and I don’t see why it shouldn’t work for all Fibonacci sequences. It’s just about int numbers being odd; how is there a way of getting a remainder other than 0 or 1 when dividing something by 2?

Actually…you’re rigth there. My mistake!
It totally works the way you did it

function isOdd(value) {
   return value % 2 === 1;
}
function sumFibs(num) {
 
 let arr = [0, 1];
  let i = 2;
  while (i <= num){
    arr.push(arr[i - 2] + arr[i - 1])
    i++;
  }
  let sum = 0;

  for (var x in arr){
    if (arr[x] % 2 !== 0 && arr[x] <= num){
      sum = sum + arr[x];
    }
  }
  return sum;
}