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
}

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?

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.

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 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.