fix(learn) : Changed language and added a test for Project euler problem-2 (#39221)

* Edited wording in a test

* Rearranged the tests

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Mainak Debnath
2020-07-26 22:39:46 +05:30
committed by GitHub
parent 25be0f379e
commit 92377cf71a

View File

@ -7,6 +7,7 @@ forumTopicId: 301838
--- ---
## Description ## Description
<section id='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: 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:
@ -14,22 +15,31 @@ Each new term in the Fibonacci sequence is generated by adding the previous two
<div style='text-align: center;'>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...</div> <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. 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> </section>
## Instructions ## Instructions
<section id='instructions'> <section id='instructions'>
</section> </section>
## Tests ## Tests
<section id='tests'> <section id='tests'>
```yml ```yml
tests: tests:
- text: <code>fiboEvenSum(10)</code> should return a number. - text: <code>fiboEvenSum(10)</code> should return a number.
testString: assert(typeof fiboEvenSum(10) === 'number'); testString: assert(typeof fiboEvenSum(10) === 'number');
- 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);
- text: <code>fiboEvenSum(10)</code> should return 10. - text: <code>fiboEvenSum(10)</code> should return 10.
testString: assert.strictEqual(fiboEvenSum(10), 10); testString: assert.strictEqual(fiboEvenSum(10), 10);
- text: <code>fiboEvenSum(34)</code> should return 44.
testString: assert.strictEqual(fiboEvenSum(34), 44);
- text: <code>fiboEvenSum(60)</code> should return 44. - text: <code>fiboEvenSum(60)</code> should return 44.
testString: assert.strictEqual(fiboEvenSum(60), 44); testString: assert.strictEqual(fiboEvenSum(60), 44);
- text: <code>fiboEvenSum(1000)</code> should return 798. - text: <code>fiboEvenSum(1000)</code> should return 798.
@ -38,14 +48,12 @@ tests:
testString: assert.strictEqual(fiboEvenSum(100000), 60696); testString: assert.strictEqual(fiboEvenSum(100000), 60696);
- text: <code>fiboEvenSum(4000000)</code> should return 4613732. - text: <code>fiboEvenSum(4000000)</code> should return 4613732.
testString: assert.strictEqual(fiboEvenSum(4000000), 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> </section>
## Challenge Seed ## Challenge Seed
<section id='challengeSeed'> <section id='challengeSeed'>
<div id='js-seed'> <div id='js-seed'>
@ -61,20 +69,20 @@ fiboEvenSum(10);
</div> </div>
</section> </section>
## Solution ## Solution
<section id='solution'>
<section id='solution'>
```js ```js
const fiboEvenSum = (number) => { const fiboEvenSum = (number) => {
if (number <= 1) { if (number <= 1) {
return 0; return 0;
} else { } else {
let evenSum = 0, prevFibNum = 1, fibNum = 2; // According to problem description our Fibonacci series starts with 1, 2 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++) { for (let i = 2; fibNum <= number; i++) {
if (fibNum % 2 == 0) { if (fibNum % 2 == 0) {
evenSum += fibNum; evenSum += fibNum;
@ -83,7 +91,7 @@ const fiboEvenSum = (number) => {
} }
return evenSum; return evenSum;
} }
} };
``` ```
</section> </section>