This repository was archived by the owner on Feb 20, 2019. It is now read-only.
File tree 4 files changed +62
-1
lines changed
4 files changed +62
-1
lines changed Original file line number Diff line number Diff line change @@ -4,4 +4,5 @@ node_modules
4
4
.nyc_output /
5
5
coverage /
6
6
npm-debug.log
7
- package-lock.json
7
+ package-lock.json
8
+ .vscode
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change @@ -65,6 +65,7 @@ import removeElementFromArray from './removeElementFromArray'
65
65
import generatePassword from './generate-password'
66
66
import tail from './tail'
67
67
import makeObjectIterable from './makeObjectIterable'
68
+ import fibonacciSum from './fibonacciSum'
68
69
69
70
export {
70
71
reverseArrayInPlace ,
@@ -134,4 +135,5 @@ export {
134
135
generatePassword ,
135
136
tail ,
136
137
makeObjectIterable ,
138
+ fibonacciSum ,
137
139
}
Original file line number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments