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

Commit

Permalink
feat(fibonacciSum): finding the sum of fibonacci series (#184)
Browse files Browse the repository at this point in the history
I've added a fibonacciSum function which return the sum of the fib series for the given number.

closes #132
  • Loading branch information
ravikishorethella authored and Kent C. Dodds committed Aug 6, 2018
1 parent 695b8b9 commit e65bd26
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ node_modules
.nyc_output/
coverage/
npm-debug.log
package-lock.json
package-lock.json
.vscode
28 changes: 28 additions & 0 deletions src/fibonacciSum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default fibonacciSum

/**
* Original Source: https://stackoverflow.com/a/31388603
*
* Function that returns the sum of a fib series
*
* @param {Number} num - A number
* @return {Number} - sum of the numbers
*/

function fibonacciSum(num) {
if (num <= 0) {
return 0
}
const fib = []
fib[0] = 0
fib[1] = 1
let sum = fib[0] + fib[1]
// remaining numbers
for (let i = 2; i <= num; i++) {
fib[i] = fib[i - 1] + fib[i - 2]
sum += fib[i]
}
return sum
}


2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import removeElementFromArray from './removeElementFromArray'
import generatePassword from './generate-password'
import tail from './tail'
import makeObjectIterable from './makeObjectIterable'
import fibonacciSum from './fibonacciSum'

export {
reverseArrayInPlace,
Expand Down Expand Up @@ -134,4 +135,5 @@ export {
generatePassword,
tail,
makeObjectIterable,
fibonacciSum,
}
30 changes: 30 additions & 0 deletions test/fibonacciSum.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import test from 'ava'
import {fibonacciSum} from '../src'

test('valid number', t => {
const num = 5
const expected = 12
const result = fibonacciSum(num)
t.is(expected, result)
})

test('invalid number', t => {
const num = -5
const expected = 0
const result = fibonacciSum(num)
t.is(expected, result)
})

test('number in quotes', t => {
const num = '10'
const expected = 143
const result = fibonacciSum(num)
t.is(expected, result)
})

test('decimal number', t => {
const num = 12.1
const expected = 376
const result = fibonacciSum(num)
t.is(expected, result)
})

0 comments on commit e65bd26

Please sign in to comment.