Skip to content

Commit d42f219

Browse files
committed
Complete sum all odd Fibonacci numbers algorithm
1 parent 9393c75 commit d42f219

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/fcc-intermediate-algorithms/fcc-intermediate-algorithms.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ From the Freecodecamp Javascript Certification Intermediate Algorithms module
1515
- [Missing Letters](#missing-letters)
1616
- [Sorted Union](#sorted-union)
1717
- [Convert HTML Entities](#convert-html-entities)
18+
- [Sum All Odd Fibonacci Numbers](#sum-all-odd-ficonacci-numbers)
1819

1920
#### Sum All Numbers In a Range
2021

@@ -252,3 +253,30 @@ export function convertHTML(str) {
252253
return newString.join('');
253254
}
254255
```
256+
257+
#### Sum All Odd Fibonacci Numbers
258+
259+
Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num.
260+
261+
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.
262+
263+
For example, `sumFibs(10)` should return 10 because all odd Fibonacci numbers less than or equal to 10 are 1, 1, 3, and 5.
264+
265+
```javascript
266+
export function sumFibs(num) {
267+
const getFibonacciNumbers = (upperBound) => {
268+
let numbers = [1, 1];
269+
if (upperBound == 0) {
270+
throw 'Fibonacci progression upper bound must be >= 1';
271+
}
272+
const total = () => numbers[numbers.length - 2] + numbers[numbers.length - 1];
273+
while (total() <= upperBound) {
274+
numbers.push(total());
275+
}
276+
return numbers;
277+
}
278+
279+
let numbers = getFibonacciNumbers(num).filter((num) => num % 2 !== 0);
280+
return numbers.reduce((acc, num) => acc + num);
281+
}
282+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export function sumFibs(num) {
2+
const getFibonacciNumbers = (upperBound) => {
3+
let numbers = [1, 1];
4+
if (upperBound == 0) {
5+
throw 'Fibonacci progression upper bound must be >= 1';
6+
}
7+
const total = () => numbers[numbers.length - 2] + numbers[numbers.length - 1];
8+
while (total() <= upperBound) {
9+
numbers.push(total());
10+
}
11+
return numbers;
12+
}
13+
14+
let numbers = getFibonacciNumbers(num).filter((num) => num % 2 !== 0);
15+
return numbers.reduce((acc, num) => acc + num);
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { sumFibs } from "../../src/fcc-intermediate-algorithms/sum_all_odd_fibonacci_numbers";
2+
3+
test('should sum all odd Fibonacci numbers', () => {
4+
expect(() => sumFibs(0)).toThrow('Fibonacci progression upper bound must be >= 1');
5+
expect(sumFibs(1)).toBe(2);
6+
expect(sumFibs(2)).toBe(2);
7+
expect(sumFibs(10)).toBe(10);
8+
});

0 commit comments

Comments
 (0)