Tell us what’s happening:
I am having issues with a lab regarding the Fibonacci Sequence. For some reason, the test case for sumFibs(4) should return a value of 5 (this pertains to the other test cases as well). However, that makes no sense as it wants all the odd numbers of the Fibonacci Sequence added up altogether then returned. Maybe my brain is burnt out because I admit this lab has burnt me out. I had to look up the best way to structure generating a Fibonacci Sequence because I couldn’t think.
Your code so far
function sumFibs(num) {
let x1 = 0, x2 = 1, nextVal;
const arr = [];
let sum = 0;
nextVal = x1 + x2;
while (nextVal <= num) {
arr.push(nextVal);
x1 = x2;
x2 = nextVal;
nextVal = x1 + x2;
}
console.log("Current Array: " + arr)
for (const element of arr) {
if (element % 2 !== 0) {
sum += element;
}
}
return sum;
}
console.log("Current Sum: " + sumFibs(4));
Console Output:
Current Array: 1,2,3
Current Sum: 4
Challenge Information:
Build an Odd Fibonacci Sum Calculator - Build an Odd Fibonacci Sum Calculator