Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Kristofer Koishigawa <scissorsneedfoodtoo@gmail.com> Co-authored-by: Beau Carnes <beaucarnes@gmail.com>
		
			
				
	
	
		
			90 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			90 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| ---
 | |
| id: 5900f36e1000cf542c50fe81
 | |
| challengeType: 5
 | |
| isHidden: false
 | |
| title: 'Problem 2: Even Fibonacci Numbers'
 | |
| forumTopicId: 301838
 | |
| ---
 | |
| 
 | |
| ## Description
 | |
| <section id='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:
 | |
| 
 | |
| <div style='text-align: center;'>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...</div>
 | |
| 
 | |
| By considering the terms in the Fibonacci sequence whose values do not exceed <code>n</code>, find the sum of the even-valued terms.
 | |
| </section>
 | |
| 
 | |
| ## Instructions
 | |
| <section id='instructions'>
 | |
| 
 | |
| </section>
 | |
| 
 | |
| ## Tests
 | |
| <section id='tests'>
 | |
| 
 | |
| ```yml
 | |
| tests:
 | |
|   - text: <code>fiboEvenSum(10)</code> should return a number.
 | |
|     testString: assert(typeof fiboEvenSum(10) === 'number');
 | |
|   - text: <code>fiboEvenSum(10)</code> should return 10.
 | |
|     testString: assert.strictEqual(fiboEvenSum(10), 10);
 | |
|   - 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);
 | |
|   - text: Your function should return an <code>even</code> value.
 | |
|     testString: assert.equal(fiboEvenSum(10) % 2 === 0, true);
 | |
| 
 | |
| ```
 | |
| 
 | |
| </section>
 | |
| 
 | |
| ## Challenge Seed
 | |
| <section id='challengeSeed'>
 | |
| 
 | |
| <div id='js-seed'>
 | |
| 
 | |
| ```js
 | |
| function fiboEvenSum(n) {
 | |
|   // You can do it!
 | |
|   return true;
 | |
| }
 | |
| 
 | |
| fiboEvenSum(10);
 | |
| ```
 | |
| 
 | |
| </div>
 | |
| 
 | |
| 
 | |
| 
 | |
| </section>
 | |
| 
 | |
| ## Solution
 | |
| <section id='solution'>
 | |
| 
 | |
| 
 | |
| ```js
 | |
| const fiboEvenSum = (number) => {
 | |
|   if (number <= 1) {
 | |
|     return 0;
 | |
|   } else {
 | |
|     let evenSum = 0, prevFibNum = 1, fibNum = 2; // According to problem description our Fibonacci series starts with 1, 2
 | |
|     for (let i = 2; fibNum <= number; i++) {
 | |
|       if (fibNum % 2 == 0) {
 | |
|         evenSum += fibNum;
 | |
|       }
 | |
|       [prevFibNum, fibNum] = [fibNum, prevFibNum + fibNum];
 | |
|     }
 | |
|     return evenSum;
 | |
|   }
 | |
| }
 | |
| ```
 | |
| 
 | |
| </section>
 |