This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
695b8b9
commit e65bd26
Showing
4 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ node_modules | |
.nyc_output/ | ||
coverage/ | ||
npm-debug.log | ||
package-lock.json | ||
package-lock.json | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |