# Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers

Tell us what’s happening:
Describe your issue in detail here.
i dont know what is wrong !!

function sumFibs(num) {
let fiboNums = [1,1];
let nextFibo = (fiboNums[fiboNums.length-2]+fiboNums[fiboNums.length-1]);
for (let i = 0; i < num; i++){
fiboNums.push(nextFibo);
(fiboNums[fiboNums.length-2]+fiboNums[fiboNums.length-1]);
}
console.log(num);
return fiboNums.filter(element => element%2 !==0).reduce((a,b) => a+b);
}

sumFibs(4);

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers

I don’t think this loop does what you want. You are computing the first num Fibonacci numbers, not the Fibonacci numbers less than or equal to num.

1 Like

Oh thank You I got it … I used while loop instead however i wanted to know if i can solve it by for loop as well . As I think while loops can be converted into For loop.

function sumFibs(num) {
let fiboNums = [1,1];
let nextFibo = (fiboNums[fiboNums.length-2]+fiboNums[fiboNums.length-1]);
while ( nextFibo <= num) {
fiboNums.push(nextFibo);
nextFibo = (fiboNums[fiboNums.length-2]+fiboNums[fiboNums.length-1]);
}
console.log(num);
return fiboNums.filter(element => element%2 !==0).reduce((a,b) => a+b);
}

sumFibs(4);

You could write it with a for loop, sure. You could also solve it with a singe loop and no array.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.