n
th term, find the sum of the even-valued terms.
+By considering the terms in the Fibonacci sequence that do not exceed the n
th term, find the sum of the even-valued terms.
fiboEvenSum(10)
should return 188.');
+ - text: fiboEvenSum(10)
should return 44.
+ testString: assert.strictEqual(fiboEvenSum(10), 44, 'fiboEvenSum(10)
should return 44.');
+ - text: fiboEvenSum(18)
should return 3382.
+ testString: assert.strictEqual(fiboEvenSum(18), 3382, 'fiboEvenSum(18)
should return 3382.');
- text: fiboEvenSum(23)
should return 60696.
testString: assert.strictEqual(fiboEvenSum(23), 60696, 'fiboEvenSum(23)
should return 60696.');
- - text: fiboEvenSum(43)
should return 1485607536.
- testString: assert.strictEqual(fiboEvenSum(43), 1485607536, 'fiboEvenSum(43)
should return 1485607536.');
- - text: Your function is not returning the correct result using our tests values.
- testString: assert.strictEqual(fiboEvenSum(18), 3382, 'Your function is not returning the correct result using our tests values.');
+ - text: fiboEvenSum(43)
should return 350704366.
+ testString: assert.strictEqual(fiboEvenSum(43), 350704366, 'fiboEvenSum(43)
should return 350704366.');
- text: Your function should return an even
value.
- testString: assert.equal(fiboEvenSum(31) % 2 === 0, true, 'Your function should return an even
value.');
+ testString: assert.equal(fiboEvenSum(10) % 2 === 0, true, 'Your function should return an even
value.');
```
@@ -62,18 +62,20 @@ fiboEvenSum(10);
```js
const fiboEvenSum = (number) => {
- let temp, sum = 0, a = 0, b = 1;
- while (number >= 0) {
- temp = a;
- a = b;
- b += temp;
- number --;
- if ((b % 2) === 0) {
- sum += b;
+ 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 sum;
+ return evenSum;
+ }
}
```