Sum All Odd Fibonacci Numbers

Hello,

My code does not cycle past number 4 because it is not equal to the sum of two previous numbers. But I cannot understand why. Could you please provide a hint.

// creating an array to work with further 
  let numArr=[0,1]
    for (let i=1;i<=num;i++){
    numArr.push(i)
  }
// initializing accumulator 
   let numSum=1
// cycling through array searching for odd(1) Fibonacci(2) numbers
   for (let x=2;x<numArr.length;x++){
    if (numArr[x]===numArr[x-1]+numArr[x-2] && numArr[x]%2===1) {
        numSum+=numArr[x]
    } else numSum=numSum
}
 return numSum;
}

console.log(sumFibs(20))```

I also try another way using Jeremy’s advice in other topic regarding this task. And have a problem excluding even numbers from the sum. Although the sum is calculated correctly (almost, have to find a decent way to add 1 to it).
The problem is that there appears to be an infinite loop. Any idea how to add conditions keeping only odd numbers?

function sumFibs(num) {
  let prevNum=0
  let currNum=1
  let sum=0
  for (currNum;currNum<num;currNum=sum){
    if(currNum % 2===1){
    sum=currNum+prevNum
     prevNum=currNum
      }  
}
return sum
}

I’d not worry about the even vs odd at first. Can you make a loop that prints every Fibonacci number less than or equal to num?

So this code worked for me.

Regarding your question Jeremy, the first part of my code does the job for odd numbers. For all Fibonacci numbers it was done by the code from my previous message.

function sumFibs(num) {
  let prevNum=0
  let sum=0
  let newArr=[0,1]
  for (let currNum=1;currNum<=num;currNum=sum){
    sum=currNum+prevNum
    newArr.push(sum)
    prevNum=currNum  
}
console.log(newArr)
let sum2=0
for (let i=1;i<newArr.length-1;i++){
  if (newArr[i]%2===1){
    sum2=sum2+newArr[i]
  }
}
return sum2
}

Hello Randall.
I thought I can do it. Thanks.

What do you guys think? Is this a legit solution or a half-arsed attempt?

Your variable name ‘sum’ I think is hiding your intent. Isn’t that really ‘nextNum’?

It’s also usually a bit of a red flag if you have a variable named like ‘sum2’. Would you easily understand this code when reading it 2 months from now?

Randell,
I apologize for misspelling your name.
Should I dispense with array and add the odd/even verification condition into first for loop?

Jeremy,

You are correct. I did not change its name when I changed my approach and tried to solve the case in two steps instead of one, so to speak. Thanks.

You definitely don’t have to use an array

What do you think?

function sumFibs(num) {
  let prevNum=0
  let sum=1
  let nextNum=0
  for (let currNum=1;nextNum<=num;currNum=nextNum){
    nextNum=currNum+prevNum
    if (nextNum%2===1 && nextNum<=num){
    sum=sum+nextNum
    }
    prevNum=currNum  
}
return sum
} 

Is the correct answer for case of sumFibs(1) 2 or 1? My code of course provide answer 2.

UPD. Already saw that the first suggested solution is the same. Thank you for helping.
Still, if you have any comments, please let me know.

small tweaks for formatting:

function sumFibs(num) {
  let sum = 1;
  let prevNum = 0;
  let nextNum = 0;
  for (let currNum = 1; nextNum <= num; currNum = nextNum) {
    nextNum = currNum + prevNum;
    if (nextNum % 2 === 1 && nextNum <= num) {
      sum = sum + nextNum;
    }
    prevNum = currNum;
  }
  return sum;
}

Question for you though, do you want sum <= num? Is that the stopping condition?

Strange, in my post I see it says nextNum<=num in the FOR statement and in the IF statement.

Ahh, you edited it! That wasn’t your original text in the post

yes, at first there was sum<=num. But it did not work for one of the cases (smart choice of cases:)). So I changed it.
What about the case of 1? Is the right answer 1 or 2?

The Fibonacci sequence is 0, 1, 1, 2, 3, 5, ..., so if your limit is 1, then the correct sum would be 2.

I guess the question of why we sum the second 1 with the first 1 instead of first 1 with 0 is a mathematical question=)

I’m not sure I understand?

The first two numbers are always f_0 = 0 and f_1 = 1. The pattern from there is that f_n = f_(n - 1) + f_(n - 2), so f_2 = f_1 + f_0 = 1 + 0 = 1, f_3 = f_2 + f_1 = 1 + 1 = 2 and so on.

1 Like

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