Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers it showing wrong answer on 75025

I have been solving this Question and I am stuck on Test case 75025 it is giving response 60696 and same output with input 75024.

I am stuck and can not identify the problem in my code.

Can you Please tell me what’s wrong with my code

Your code so far

function sumFibs(num) {
  let a =0;
  let b = 1;
  let sum = 0
  while (num){
    //if(b%2 != 0) sum+=b;
    //if(a%2 != 0) sum+=a;
    console.log(a)
    console.log(b)
    console.log("sum",sum)
    if(a<num) if(a%2!=0)sum+=a
    if(b<num) if(b%2!=0)sum+=b
    
    a = a+b;
    b = a+b;
    if(a>num && b>num){
      break
    }
  }
  console.log("sum",sum)
  return sum;
}

sumFibs(75025);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/117.0

Challenge: Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers

Link to the challenge:

So, what does the while loop actually do? In the slightly unconventional way of defining the fibonacci sequence, you can think of 0 and 1 as the first and second in the sequence. So the first time the body is executed, an and b hold the first two fibonacci numbers. As long as the break statement is not executed, the loop will continue to be executed, each time taking these two fibonacci numbers, checking if they qualify (are they odd?) and if so summing them. It then generates the next candidates and if these both are larger than the requested number, the loop is ended. ‘Worst case’, a is smaller or equal to num while b is already larger. No biggie, just run the loop again.

It seems that this is iron-clad. But something goes wrong. It’s not in the if statement; that is fine. As long as the newly generated fibonacci numbers are in range (<=num) you should ‘process’ them.

But it’s in the ‘processing’ that things are wrong. When does a number (either a or b) qualify to be added to the sum? When it’s odd, and that is checked. Can’t be that…what could it be?

0 and 1 are the conventional first two numbers in the sequence.

https://oeis.org/A000045

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