Hello guys!
I am stuck with this challenge, I am a newbie so I made up this and it only pass 5/6 and I don’t really know what to do anymore. Can you please take a look?
Thanks in advance.
function sumFibs(num) {
let prev = 1;
let curr = 1;
let next;
var sumofOdds = [];
sumofOdds.push(prev);
sumofOdds.push(curr);
for(let i = 0; i<num; i++) {
next = prev+curr;
prev = curr;
curr = next;
// console.log(next)
sumofOdds.push(next)
// console.log(sumofOdds)
};
var odds = sumofOdds.filter((item) => item % 2 === 1);
// console.log(odds);
var total = 0;
for(let i = 0; i<odds.length - 1;i++) {
while (total < num) {
total +=odds[i];
i++;
};
};
return total;
}
console.log(sumFibs(75025));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36
Challenge: Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers
" Given a positive integer num , return the sum of all odd Fibonacci numbers that are less than or equal to num ." and : " For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less than or equal to 10 are 1, 1, 3, and 5. "
Good work! Now that you have something that works, I think this is a good challenge to compare the posted solutions with your own - there are some ways to be a bit more efficient.