0

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers

I need to complete theses task, so I have made a function to sum Fibonacci series. But I need to sum until I get the number which is asked for by the program. Can you make any suggestions? Please, correct my english if it is necessary and help me to get the answer for my code.

sumFibs(1000) should return 1785. sumFibs(4000000) should return 4613732. sumFibs(75024) should return 60696. sumFibs(75025) should return 135721.

function sumFibs(num) {

let numArr=[];

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

   numArr.push(i)

   }  
console.log()
let oddNumArr=numArr.filter(numbers=>numbers % 2 !== 0) 

oddNumArr.unshift(1)

let reducedArr=oddNumArr.reduce((a, b) => a + b, 0)

return reducedArr;
console.log(reducedArr)

  • Well, firstly your not generating a Fibonacci sequence with your code, just a normal number sequence. Start by fixing that and see what goes wrong next. – Milton Apr 30 '20 at 22:34
  • The rest of your code is fine, it's just the generation of the sequence that isn't right. If num is 10, you should generate [0, 1, 1, 2, 3, 5, 8]. Another little tip is to unlearn adding Arr to every array, instead give the set of items a better name. numbers is better than numArray, but even better would be fibonacciNumbers. Also reduce returns a value, not an array, but the value can be of any type, in your case it's a number since you give 0 as the starting point and then add numbers to that. – Milton Apr 30 '20 at 22:45
  • Hi, Aaron, you need to make a Fibonacci sequence and then should sum one by one after validating each one is an odd or not. Reducer function usage a little bit tricky here, if you update your question like ** How to sum all odd Fibonacci numbers ** and you want to get the right optimized code for that finally in this topic, I am going to answer with the right code down below. Thanks. – Kevin Lee May 01 '20 at 07:06

3 Answers3

1

Here is the optimized code that you want finally.

function sumFibs(num) {
  let m = 0;
  let n = 1;
  let count = 0;

  while (n < num) {
    [n, m] = [n+m, n];

    if (m%2 !== 0) {
      count+=m;
    }
  }
  return count;
}
console.log(sumFibs(1000));
console.log(sumFibs(4000000));
console.log(sumFibs(75024));
console.log(sumFibs(75025));

If you are in need of reduce function, you should go through a little bit more lines down in your code.

Here is the code that uses reduce function

function sumFibs(num) {
  let lastFib=0;
  let fibonacci=[0,1]; 
  let i=0;
  while(lastFib<num){
      lastFib=fibonacci[i]+fibonacci[i+1];
      fibonacci.push(lastFib);
      i++;
  }  
  fibonacci.pop();
  return fibonacci.filter(x=>x%2!==0).reduce((a,b)=>a+b);
}

console.log(sumFibs(1000));
console.log(sumFibs(4000000));
console.log(sumFibs(75024));
console.log(sumFibs(75025));

Hopefully, this would be helpful, please comment down below if you have any questions! Thanks.

Kevin Lee
  • 332
  • 2
  • 13
0
function sumFibs(num) {



let  numArr = [1, 1];

for(let i=0;num>=numArr[numArr.length-1];i++){

let y=numArr[0+i] +numArr[1+i]

numArr.push(y) 

}

 console.log(numArr)

 let oddNumArr=numArr.filter(numbers=>numbers % 2 !== 0) 
   let lessThanNumArr=oddNumArr.filter(n=>n<=num)  
console.log(oddNumArr)

let reducedArr=lessThanNumArr.reduce((a, b) => a + b, 0)

console.log(reducedArr)

return reducedArr;

}

sumFibs(75025);
0
function Fibs(item){

  if (item==0 || item==1){
    return 1;
  }
  else{
    return Fibs(item-1)+Fibs(item-2);
  }

}

function sumFibs(num) {

  let item = 0;
  let sum = 0;

  while (Fibs(item)<=num){

    if (Fibs(item)%2 > 0){
      sum = sum + Fibs(item);
    }
    item++;
  }

  
  return sum;
}