--- id: 5900f36e1000cf542c50fe81 challengeType: 5 title: 'Problem 2: Even Fibonacci Numbers' forumTopicId: 301838 --- ## Description
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence that do not exceed the nth term, find the sum of the even-valued terms.
## Instructions
## Tests
```yml tests: - text: fiboEvenSum(10) should return 44. testString: assert.strictEqual(fiboEvenSum(10), 44); - text: fiboEvenSum(18) should return 3382. testString: assert.strictEqual(fiboEvenSum(18), 3382); - text: fiboEvenSum(23) should return 60696. testString: assert.strictEqual(fiboEvenSum(23), 60696); - text: fiboEvenSum(43) should return 350704366. testString: assert.strictEqual(fiboEvenSum(43), 350704366); - text: Your function should return an even value. testString: assert.equal(fiboEvenSum(10) % 2 === 0, true); ```
## Challenge Seed
```js function fiboEvenSum(n) { // You can do it! return true; } fiboEvenSum(10); ```
## Solution
```js const fiboEvenSum = (number) => { if (number <= 1) { return 0; } else { let evenSum = 2, first = 1, second = 2, fibNum; // According to problem description our Fibonacci series starts with 1, 2 for (let i = 3; i <= number; i++) { fibNum = first + second; first = second; second = fibNum; if (fibNum % 2 == 0) { evenSum += fibNum; } } return evenSum; } } ```