Hello ,
I am trying to solve this issue.
" Given a positive integer num
, return the sum of all odd Fibonacci numbers that are less than or equal to num
.
The first two numbers in the Fibonacci sequence are 1 and 1. Every additional number in the sequence is the sum of the two previous numbers. The first six numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8.
For example, sumFibs(10)
should return 10
because all odd Fibonacci numbers less than or equal to 10
are 1, 1, 3, and 5."
This was my solution, and I just can’t understand why it satisfies all the statements except the last one. I can’t find the mistake.
I would appreciate any help!
function sumFibs(num) {
if(num === 1){
return 1;
}else if(num ===2){
return 2;
}
let arr=[1,1],
curr =1,
prev =1;
for(let i = 2; i<num;i++){
let next = curr + prev;
prev = curr;
curr= next;
if(curr<num && curr%2!==0){
arr.push(curr);
}
}
return arr.reduce((a,b)=>a+b);
}
console.log(sumFibs(75024));
console.log(sumFibs(75025))
![Screenshot 2020-12-03 170937|559x500](upload://oa0YUvqj2MstfJv4VyvI4g4GIvS.jpeg)