Tell us what’s happening:
Code works for all the tests except the last one
Your code so far
function sumFibs(num) {
let initial = 1;
let next = 2;
let sum = 2;
let temp = 0;
while(num > temp)
{
if(temp % 2 !== 0)
{
sum += temp;
}
temp = initial + next;
initial = next;
next = temp;
console.log(sum);
}
return sum;
}
sumFibs(10);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36
.
Challenge: Sum All Odd Fibonacci Numbers
Link to the challenge:
You have a small issue with your loop condition.
From the challenge description
For example, sumFibs(10)
should return 10
because all odd Fibonacci numbers less than or equal to 10
are 1, 1, 3, and 5.
But you have
(num > temp)
Also, it would make it much clearer if you used next
in your loop condition instead of temp
. (num > next
)
Also (also also?) its typical to see the value that changes on the left and the value that’s constant on the right (next < num
).
1 Like