Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit e65bd26

Browse files
ravikishorethellaKent C. Dodds
authored and
Kent C. Dodds
committed
feat(fibonacciSum): finding the sum of fibonacci series (#184)
I've added a fibonacciSum function which return the sum of the fib series for the given number. closes #132
1 parent 695b8b9 commit e65bd26

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ node_modules
44
.nyc_output/
55
coverage/
66
npm-debug.log
7-
package-lock.json
7+
package-lock.json
8+
.vscode

Diff for: src/fibonacciSum.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export default fibonacciSum
2+
3+
/**
4+
* Original Source: https://stackoverflow.com/a/31388603
5+
*
6+
* Function that returns the sum of a fib series
7+
*
8+
* @param {Number} num - A number
9+
* @return {Number} - sum of the numbers
10+
*/
11+
12+
function fibonacciSum(num) {
13+
if (num <= 0) {
14+
return 0
15+
}
16+
const fib = []
17+
fib[0] = 0
18+
fib[1] = 1
19+
let sum = fib[0] + fib[1]
20+
// remaining numbers
21+
for (let i = 2; i <= num; i++) {
22+
fib[i] = fib[i - 1] + fib[i - 2]
23+
sum += fib[i]
24+
}
25+
return sum
26+
}
27+
28+

Diff for: src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import removeElementFromArray from './removeElementFromArray'
6565
import generatePassword from './generate-password'
6666
import tail from './tail'
6767
import makeObjectIterable from './makeObjectIterable'
68+
import fibonacciSum from './fibonacciSum'
6869

6970
export {
7071
reverseArrayInPlace,
@@ -134,4 +135,5 @@ export {
134135
generatePassword,
135136
tail,
136137
makeObjectIterable,
138+
fibonacciSum,
137139
}

Diff for: test/fibonacciSum.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import test from 'ava'
2+
import {fibonacciSum} from '../src'
3+
4+
test('valid number', t => {
5+
const num = 5
6+
const expected = 12
7+
const result = fibonacciSum(num)
8+
t.is(expected, result)
9+
})
10+
11+
test('invalid number', t => {
12+
const num = -5
13+
const expected = 0
14+
const result = fibonacciSum(num)
15+
t.is(expected, result)
16+
})
17+
18+
test('number in quotes', t => {
19+
const num = '10'
20+
const expected = 143
21+
const result = fibonacciSum(num)
22+
t.is(expected, result)
23+
})
24+
25+
test('decimal number', t => {
26+
const num = 12.1
27+
const expected = 376
28+
const result = fibonacciSum(num)
29+
t.is(expected, result)
30+
})

0 commit comments

Comments
 (0)