2018-09-30 23:01:58 +01:00
---
id: a5229172f011153519423690
title: Sum All Odd Fibonacci Numbers
challengeType: 5
2019-07-31 11:32:23 -07:00
forumTopicId: 16084
2021-01-13 03:31:00 +01:00
dashedName: sum-all-odd-fibonacci-numbers
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
Given a positive integer `num` , return the sum of all odd Fibonacci numbers that are less than or equal to `num` .
2018-09-30 23:01:58 +01:00
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.
2020-11-27 19:02:05 +01:00
For example, `sumFibs(10)` should return `10` because all odd Fibonacci numbers less than or equal to `10` are 1, 1, 3, and 5.
# --hints--
`sumFibs(1)` should return a number.
```js
assert(typeof sumFibs(1) === 'number');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`sumFibs(1000)` should return 1785.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(sumFibs(1000) === 1785);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`sumFibs(4000000)` should return 4613732.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(sumFibs(4000000) === 4613732);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`sumFibs(4)` should return 5.
```js
assert(sumFibs(4) === 5);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`sumFibs(75024)` should return 60696.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(sumFibs(75024) === 60696);
```
`sumFibs(75025)` should return 135721.
```js
assert(sumFibs(75025) === 135721);
```
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
function sumFibs(num) {
return num;
}
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
sumFibs(4);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
function sumFibs(num) {
2018-10-08 01:01:53 +01:00
var a = 1;
2018-09-30 23:01:58 +01:00
var b = 1;
var s = 0;
while (a < = num) {
2018-10-08 01:01:53 +01:00
if (a % 2 !== 0) {
s += a;
2018-09-30 23:01:58 +01:00
}
a = [b, b=b+a][0];
}
return s;
}
```