-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 9 - SumOddFibonacci.js
75 lines (55 loc) · 1.73 KB
/
Day 9 - SumOddFibonacci.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*Sum Odd Fibonacci Numbers
https://scrimba.com/scrim/co2424333a0210f45fc8b2795
DESCRIPTION:
Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num.
The first two numbers in the Fibonacci sequence are 1 and 1. Every additional number in the sequence is the sum of the two previous numbers.
The first six numbers of the Fibonacci sequence are 1,1,2,3,5 and 8.
For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less than or equal to 10 are 1,1,3 and 5.
Example:
- sumOddFibonacciNums(10) should return 10
- sumOddFibonacciNums(1000) should return 1785
- sumOddFibonacciNums(4000000) should return 4613732
*/
function fibonacci(position) {
if (position < 2) {
return 1
}
return fibonacci(position-2) + fibonacci(position-1)
}
function sumOddFibonacciNumbers(num) {
let sum = 0
let count = 0
while(sum < num) {
let fibo = fibonacci(count)
if(fibo % 2 !== 0) {
sum += fibo
}
count++
}
return sum
}
/**
* Test Suite
*/
describe('sumOddFibonacciNumbers()', () => {
it('returns sum of all odd Fibonnci numbers', () => {
// arrange
const num = 10;
// act
const result = sumOddFibonacciNumbers(num);
// log
console.log("result 1: ", result);
// assert
expect(result).toBe(10);
});
it('returns sum of all odd Fibonnci numbers 2nd example', () => {
// arrange
const num = 1000;
// act
const result = sumOddFibonacciNumbers(num);
// log
console.log("result 2: ", result);
// assert
expect(result).toBe(1785);
});
});