Sum All Odd Fibonacci Numbers - single test failing

Tell us what’s happening:

Howdy! I’m attempting to sum the odd fibonacci numbers using a while loop. All tests are passing except for 75024 and I’m out of ideas as to why. I’ve set three variables as the prior number, current number, and fibonacci. While inside the loop current is being added to prior, with result being used as the next number used to calculate the odd digits. If result is odd it is added to fibonacci, if not it is skipped until the next odd number. Prior to entering the if statment, both the prior number is updated to current and the current number is now updated to be the result. The loop runs until fibonacci is greater than or equal to num.

I’ve adjusted the conditions inside the while loop but am plumb out of ideas as to why the code is not working. It’s interesting that 75024 and 75025 return the same result, which leads me to believe it may be the condition of my while loop. If anyone could help me understand the error in my logic it would be much appreciated. Thanks!

function sumFibs(num) {
var prior = 0;
var current = 1;
var fibonacci = 1;
while(fibonacci <= num){
    var result = prior + current; 
    prior = current;
    current = result;
    if(result % 2 != 0){
        fibonacci += result;
}return fibonacci;

Link to the challenge:

OK, you are on the right track, but you have a slight problem in the order of your logic. In your while loop, you are jumping to the next number, then you’re checking if it is an odd number and adding it. Because of that you end up adding the next fib number even if it will fail the next while loop. That’s why it is failing that test case - it is adding the next number even though it shouldn’t. So, I would suggest the following changes:

  1. Move the if statement to the top of the while loop.
  2. Your if statement should be checking against current (result won’t have been generated yet, and besides, it’s the next fib number.) And add current to the running total, not result, for the same reasons.
  3. Your while statement should be seeing if current is less than or equal to num, not fibonacci. I think fibonacci is a bad variable name because it is not the fibonacci number, it is the sum of odd fibs. I would call it sum or sumOdds or sumOddFibs - that makes it clear exactly what it is, and won’t cause confusion. Variable naming is important. Even if you know what it means, others won’t and if you come back to maintain the code in a few months you won’t either. This caused some confusion for me when I looked at your code. It may have confused you a bit too because you were using it incorrectly.
  4. Your sum (fibonacci or sumOdds or whatever) needs to start at 0. Your starting at 1 sort of worked for many cases because you were comparing the next fib and not the current one. Now that you are comparing the current one, you don’t need to start at 1 to account for the missing first one.

When I make these changes, it passes.

Thans @kevinSmith! I can’t believe I had it backwards but it makes sense now. I was adding the number ahead of time, which makes this all the more frustrating because I feel like I should have known better. Thank you again for your explanation, I’ll definitely remember some lessons from this one…