Why does it include the last one (75025)? is it a problem with integers/floats?(I don't think so but)

I solved this exercise before, I just wanted to have a finer/shorter solution.

In this code, I don’t know why, but while checking sumFibs(75024); the fibonacci array is including number 75025.

It shouldn’t but it is. I really don’t know why.

I am not sure if I have to blur it but anyway;
the code is as follows:

function sumFibs(num) {
  let num1 = 0,
    num2 = 1,
    nextOne;
  var fibArr = [];
  var total = 0;
  var sum = 0;
  if (num === 0) {
    return 0;
  }
  for (let i = 0; i < num && sum <= num; i++) {
    if (num1 % 2 !== 0) {
      fibArr.push(num1);
    }
    sum = fibArr.reduce((partSum, a) => partSum + a, 0);
    nextOne = num1 + num2;
    num1 = Math.floor(num2);
    num2 = Math.floor(nextOne);
  }

  console.log(fibArr, sum);
  return sum;
}
sumFibs(75024);
sumFib(75025);

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

thanks for your help.

I’d look at something smaller.

sumFibs(4)

You are computing an array of [1, 1], but the correct array is [1, 1, 2, 3]. So what’s stopping you early?

Oh, I didn’t realize this one. It should include 3, so the correct array should be [1,1,3] as I omit even numbers while I’m creating the array. but 3 isn’t there.

I guess it because I am trying to create an array without even numbers first. but sum<=num equation doesn’t work here and I should change it.

let me try a quick change and return back.

1 Like

I’d look at something smaller.

sumFibs(4)

You are computing an array of [1, 1], but the correct array is [1, 1, 2, 3]. So what’s stopping you early?

I forgot to put = sign in the equation, so it solved the sumFibs(4) problem, but sumFibs(75024) still standing there.

for i=75024, 75024<=75024&& 60696 <= 75024 -----> okay, then 75024%2= 0 so don’t push it, exit if, then reduce to find sum = 60696, then rearrange,
i=75025 which is not less then or equal to num= 75024, exit for loop.

when is number 75025 joining to fibArr??
something is happening after 60696, that I couldn’t understand.

While I am writing this, I am still trying to figure out and to make changes…

Your loop says that you are stopping creating Fibonacci numbers when i <= num && sum <= num, but the instruction say the sum of all odd Fibonacci numbers that are less than or equal to num. I don’t think a for loop is what you want. You want a loop that lets you stop when the most recent Fibonacci number is not less than or equal to num.

Oh, totally wrong equation.
Trying to correct it now.
Thanks a lot.

1 Like

Solved it at last!

I first made the fibonacci array, then removed even numbers,
then created a new array by pushin until the sum is less than or equal to num.

Can you shortly comment on it?

function sumFibs(num) {
  let num1 = 0,
    num2 = 1,
    nextOne;
  var fibArr = [];
  var anyArr =[];
  var result= 0;
  
  if (num === 0) { 
    return 0;
  }
  
  for (let i = 0;i <= num; i++) {
    fibArr.push(num1);
    nextOne = Math.floor(num1 + num2);
    num1 = Math.floor(num2);
    num2 = Math.floor(nextOne);
  }

var newArr= fibArr.filter(x => x % 2 != 0)

for( let j=0; newArr[j] <= num ;j++)
{ anyArr.push(newArr[j]);}

result = anyArr.reduce((a,b) => a+b)

return result;
}
sumFibs(1000);

thanks a lot!

Good work getting it passing!

A few comments:

  1. Math.floor() is not needed - you only have integers

  2. var is a legacy feature - use const where you can and let where you can’t use const

  3. You are making 3 different huge arrays when you only need 0 arrays for this problem.

Here you are making a massive array with num Fibonacci numbers - you only need the Fibonacci numbers that are less than or equal to num

Rule of thumb - only store data if you need it more than once. You only need the Fibonacci numbers once - to sum up the odd ones - so you don’t need an array.

This one is a bit tricky/mathy to do the canonical ‘right way’.

1 Like

thank you very much for your comments,
Really appreciated!

I know I am always creating lots of variables (and arrays), I will refine my code better with your comments.

Thank you, have a nice day!

It takes practice. You’ll get there!