2018-09-30 23:01:58 +01:00
---
id: a5229172f011153519423690
title: Sum All Odd Fibonacci Numbers
isRequired: true
challengeType: 5
2019-07-31 11:32:23 -07:00
forumTopicId: 16084
2018-09-30 23:01:58 +01:00
---
## Description
<section id='description'>
Given a positive integer <code>num</code>, return the sum of all odd Fibonacci numbers that are less than or equal to <code>num</code>.
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, <code>sumFibs(10)</code> should return <code>10</code> because all odd Fibonacci numbers less than or equal to <code>10</code> are 1, 1, 3, and 5.
</section>
## Instructions
<section id='instructions'>
</section>
## Tests
<section id='tests'>
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: <code>sumFibs(1)</code> should return a number.
2019-07-24 01:56:38 -07:00
testString: assert(typeof sumFibs(1) === "number");
2018-10-04 14:37:37 +01:00
- text: <code>sumFibs(1000)</code> should return 1785.
2019-07-24 01:56:38 -07:00
testString: assert(sumFibs(1000) === 1785);
2018-10-04 14:37:37 +01:00
- text: <code>sumFibs(4000000)</code> should return 4613732.
2019-07-24 01:56:38 -07:00
testString: assert(sumFibs(4000000) === 4613732);
2018-10-04 14:37:37 +01:00
- text: <code>sumFibs(4)</code> should return 5.
2019-07-24 01:56:38 -07:00
testString: assert(sumFibs(4) === 5);
2018-10-04 14:37:37 +01:00
- text: <code>sumFibs(75024)</code> should return 60696.
2019-07-24 01:56:38 -07:00
testString: assert(sumFibs(75024) === 60696);
2018-10-04 14:37:37 +01:00
- text: <code>sumFibs(75025)</code> should return 135721.
2019-07-24 01:56:38 -07:00
testString: assert(sumFibs(75025) === 135721);
2018-09-30 23:01:58 +01:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function sumFibs(num) {
return num;
}
sumFibs(4);
```
</div>
</section>
## Solution
<section id='solution'>
```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;
}
```
</section>