Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers

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

code

function sumFibs(num) {
  let farr= [0,1]
  let fnum= 0;
  let farr1=[0,1]


  for( let i=2; i<num+1 ;i++){
    farr[i]= farr[i-1]+farr[i-2];
    if(farr[i]>=num){
      i=num+1;
    }

  }

  for(let i= 2 ; i< farr.length;i++){
    
    if(farr[i] %2 !==0){
      farr1.push(farr[i])

    }
  }
  console.log(farr);
  console.log(farr1);


for(let i=0; i<farr1.length; i++){
  if(farr1[i]<=num){
fnum+=farr1[i];
  }
  console.log(fnum)
  
}
  
  
  return fnum;
}

sumFibs(1000);

i wanted to use the reduce method here in order to get my result however its causing a return of undefined on half of the tests and i am unsure as to why.
i ended upusing a for loop to finish it but i would like to get handy with the reduce method, can someone tell me why this

for(let i=0; i<farr1.length; i++){
  if(farr1[i]<=num){
fnum+=farr1[i];
  }
  console.log(fnum)
  
}
  
  
  return fnum;

and this

fnum= farr1.reduce((acc,elem) => { if(elem<=num){
    console.log(acc);return acc+elem;} },0);
  console.log(fnum);
  

are not producing the same results? p.s the logs are me trying to debug

Hey

First of all, it would be nice to format your code so you and others can read it easily.

I did this by using Prettier Playground
I also removed all console.log

fnum = farr1.reduce((acc, elem) => {
  if (elem <= num) {
    return acc + elem;
  }
}, 0);

So when the elem <= num, the function returns their sum.
But what does the funtion return if the elem > num ?

1 Like

i see so the else is necessary otherwise we return undef. so if i added else return acc then it would fix?

Correct!

However, you don’t need else block. If the condition passes in the first if block, the code won’t continue any further.
You can write it just like this:

fnum = farr1.reduce((acc, elem) => {
  if (elem <= num) {
    return acc + elem;
  }
  return acc;
}, 0);

And remember:

function sayHello() {
  console.log("Hello!");
}

let resultOfFunctionCall = sayHello() // undefined

function sayBye() {
  console.log("Bye");
  return;
}

let resultOfFunctionCall = sayBye() // undefined

Remember to always return some value using reduce().

What happened here is that you did not return any value in a case when elem > num, which returned undefined by default and any number you tried to add later with undefined results in undefined.

10 + undefined // = undefined

i used the reduce method in this challange ;

and it works thank you .

i would like to ask 1 more thing if i may ,

i did the reduce method with the if else and it works fine however i tried going a step further syntax wise and substituting that for a ternary expression and i seem to be getting undefined again.

this is my code

let fnum =farr.reduce((acc,elem)  => {elem<=num ?  acc+elem : acc;},0);

and i tried adding a return after the ternary expression in order to account for the mistake i was doing before however the result of it :

let fnum =farr.reduce((acc,elem)  => {elem<=num? acc+elem: acc;return acc;},0);

is 0 on every instance , can ternary not substitute if under some conditions?

all my code if necessary

function sumPrimes(num) {

  let arr=[2];
  let farr=[]
  for(let i=3; i<=num;i++){
    if (i % 2!== 0 ){
      arr.push(i)
    }
    for(let i=1; i<arr.length; i++){
      let pushable = true;
      for(let j=1;j<arr.length;j++){
      if(arr[i]!==arr[j]){
        if(arr[i] % arr[j]==0){
          pushable=false;
        }
      }


    }
    if(!pushable){
      arr[i]=0;
    }
    }

    for(let i=0; i<num ;i++){
      
    }
  }
  farr= arr.filter(elem => elem!==0 )
  console.log(farr)

  let fnum =farr.reduce((acc,elem)  => {elem<=num? acc+elem: acc;return acc;},0);
  console.log(fnum)
  return fnum;
}

sumPrimes(10);

Again, format your code and you will see your mistake.

let fnum = farr.reduce((acc, elem) => {
  elem <= num ? acc + elem : acc;
  return acc;
}, 0);

And please, don’t try to use all the fancy syntax if it makes reading the code more difficult. It’s not worth it.

i found it i must return the result of the teranry by adding return to the front ,and i will keep your advice in mind thank you so much!

Like this

let fnum = farr.reduce((acc, elem) => {
  return elem <= num ? acc + elem : acc;
}, 0);

What to take away from this conversation?

  1. Format your code!
  2. When you think this conversation is finished, click on the Solution button.
    image

We don’t really encourage asking for check marks here. One of the mods will mark the topic as solved if it’s needed.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.