1function sumFibs(num) {
2 var prevNumber = 0;
3 var currNumber = 1;
4 var result = 0;
5 while (currNumber <= num) {
6 if (currNumber % 2 !== 0) {
7 result += currNumber;
8 }
9
10 currNumber += prevNumber;
11 prevNumber = currNumber - prevNumber;
12 }
13
14 return result;
15}
16
17// test here
18sumFibs(4);
19
1const sumFibs = num => {
2 // Fibonacci always starts with a [1,1] so, we just initialize it
3 const fibNum = [1,1] // Get all the fibonacci numbers for that range
4 const fibOdd = [1,1] // Get all the odd fibonacci numbers for that range
5
6 // Loop for through for n times(num)
7 for(let i = 0; i < num; i++){
8 // Add the current index to the next index to get the later fib number
9 const fibAdd = fibNum[i] + fibNum[fibNum.length - 1]
10
11 if(fibAdd > num) break; // Stop pushing or looping if added indexes is more than number(num)
12 fibNum.push(fibAdd) // Push to all fibNum if added indexes is less than the number(num)
13
14 // Push odd fib numbers to fibOdd, so we can now uniquely add only odd numbers
15 if(fibAdd % 2 !== 0) fibOdd.push(fibAdd)
16 }
17
18 // Return reduced or added fibonacci odd numberss
19 return fibOdd.reduce((acc,cur) => acc + cur)
20}
21
22sumFibs(75024);
23
24// With love @kouqhar