2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
id: 5900f36e1000cf542c50fe81
|
|
|
|
challengeType: 5
|
|
|
|
title: 'Problem 2: Even Fibonacci Numbers'
|
2019-08-05 09:17:33 -07:00
|
|
|
forumTopicId: 301838
|
2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-07-26 22:39:46 +05:30
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
<section id='description'>
|
2020-02-28 21:39:47 +09:00
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
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:
|
2020-02-28 21:39:47 +09:00
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
<div style='text-align: center;'>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...</div>
|
2020-02-28 21:39:47 +09:00
|
|
|
|
2020-02-07 21:39:35 +05:30
|
|
|
By considering the terms in the Fibonacci sequence whose values do not exceed <code>n</code>, find the sum of the even-valued terms.
|
2020-07-26 22:39:46 +05:30
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
2020-07-26 22:39:46 +05:30
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
<section id='instructions'>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
2020-07-26 22:39:46 +05:30
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
2018-10-04 14:37:37 +01:00
|
|
|
tests:
|
2020-02-28 21:39:47 +09:00
|
|
|
- text: <code>fiboEvenSum(10)</code> should return a number.
|
|
|
|
testString: assert(typeof fiboEvenSum(10) === 'number');
|
2020-07-26 22:39:46 +05:30
|
|
|
- text: Your function should return an <code>even</code> value.
|
|
|
|
testString: assert.equal(fiboEvenSum(10) % 2 === 0, true);
|
|
|
|
- text: 'Your function should sum the even-valued Fibonacci numbers: <code>fiboEvenSum(8)</code> should return 10.'
|
|
|
|
testString: assert.strictEqual(fiboEvenSum(8), 10);
|
2020-02-07 21:39:35 +05:30
|
|
|
- text: <code>fiboEvenSum(10)</code> should return 10.
|
|
|
|
testString: assert.strictEqual(fiboEvenSum(10), 10);
|
2020-07-26 22:39:46 +05:30
|
|
|
- text: <code>fiboEvenSum(34)</code> should return 44.
|
|
|
|
testString: assert.strictEqual(fiboEvenSum(34), 44);
|
2020-02-07 21:39:35 +05:30
|
|
|
- text: <code>fiboEvenSum(60)</code> should return 44.
|
|
|
|
testString: assert.strictEqual(fiboEvenSum(60), 44);
|
|
|
|
- text: <code>fiboEvenSum(1000)</code> should return 798.
|
|
|
|
testString: assert.strictEqual(fiboEvenSum(1000), 798);
|
|
|
|
- text: <code>fiboEvenSum(100000)</code> should return 60696.
|
|
|
|
testString: assert.strictEqual(fiboEvenSum(100000), 60696);
|
|
|
|
- text: <code>fiboEvenSum(4000000)</code> should return 4613732.
|
|
|
|
testString: assert.strictEqual(fiboEvenSum(4000000), 4613732);
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
2020-07-26 22:39:46 +05:30
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function fiboEvenSum(n) {
|
2020-09-15 12:31:21 -07:00
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
2020-07-26 22:39:46 +05:30
|
|
|
<section id='solution'>
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
const fiboEvenSum = (number) => {
|
2019-01-19 15:20:53 +05:30
|
|
|
if (number <= 1) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
2020-07-26 22:39:46 +05:30
|
|
|
let evenSum = 0,
|
|
|
|
prevFibNum = 1,
|
|
|
|
fibNum = 2; // According to problem description our Fibonacci series starts with 1, 2
|
2020-02-07 21:39:35 +05:30
|
|
|
for (let i = 2; fibNum <= number; i++) {
|
|
|
|
if (fibNum % 2 == 0) {
|
|
|
|
evenSum += fibNum;
|
2018-09-30 23:01:58 +01:00
|
|
|
}
|
2020-02-07 21:39:35 +05:30
|
|
|
[prevFibNum, fibNum] = [fibNum, prevFibNum + fibNum];
|
|
|
|
}
|
|
|
|
return evenSum;
|
2019-01-19 15:20:53 +05:30
|
|
|
}
|
2020-07-26 22:39:46 +05:30
|
|
|
};
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|