| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 5900f36e1000cf542c50fe81 | 
					
						
							|  |  |  | challengeType: 5 | 
					
						
							|  |  |  | title: 'Problem 2: Even Fibonacci Numbers' | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## 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> | 
					
						
							| 
									
										
										
										
											2019-01-19 15:20:53 +05:30
										 |  |  | By considering the terms in the Fibonacci sequence that do not exceed the <code>n</code>th term, find the sum of the even-valued terms. | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | </section> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Instructions
 | 
					
						
							|  |  |  | <section id='instructions'> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </section> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Tests
 | 
					
						
							|  |  |  | <section id='tests'> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```yml | 
					
						
							| 
									
										
										
										
											2018-10-04 14:37:37 +01:00
										 |  |  | tests: | 
					
						
							| 
									
										
										
										
											2019-01-19 15:20:53 +05:30
										 |  |  |   - text: <code>fiboEvenSum(10)</code> should return 44. | 
					
						
							|  |  |  |     testString: assert.strictEqual(fiboEvenSum(10), 44, '<code>fiboEvenSum(10)</code> should return 44.'); | 
					
						
							|  |  |  |   - text: <code>fiboEvenSum(18)</code> should return 3382. | 
					
						
							|  |  |  |     testString: assert.strictEqual(fiboEvenSum(18), 3382, '<code>fiboEvenSum(18)</code> should return 3382.'); | 
					
						
							| 
									
										
										
										
											2018-10-04 14:37:37 +01:00
										 |  |  |   - text: <code>fiboEvenSum(23)</code> should return 60696. | 
					
						
							| 
									
										
										
										
											2018-10-20 21:02:47 +03:00
										 |  |  |     testString: assert.strictEqual(fiboEvenSum(23), 60696, '<code>fiboEvenSum(23)</code> should return 60696.'); | 
					
						
							| 
									
										
										
										
											2019-01-19 15:20:53 +05:30
										 |  |  |   - text: <code>fiboEvenSum(43)</code> should return 350704366. | 
					
						
							|  |  |  |     testString: assert.strictEqual(fiboEvenSum(43), 350704366, '<code>fiboEvenSum(43)</code> should return 350704366.'); | 
					
						
							| 
									
										
										
										
											2018-10-04 14:37:37 +01:00
										 |  |  |   - text: Your function should return an <code>even</code> value. | 
					
						
							| 
									
										
										
										
											2019-01-19 15:20:53 +05:30
										 |  |  |     testString: assert.equal(fiboEvenSum(10) % 2 === 0, true, 'Your function should return an <code>even</code> value.'); | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </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) => { | 
					
						
							| 
									
										
										
										
											2019-01-19 15:20:53 +05:30
										 |  |  |   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; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-01-19 15:20:53 +05:30
										 |  |  |       return evenSum; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </section> |