Skip to content

Commit 2664ca5

Browse files
committed
fibonacci spec and throw on too large of input.
1 parent d60b8b8 commit 2664ca5

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/others/fibonacci.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
'use strict';
2020

2121
function fibonacci (n) {
22+
if (n > 97) {
23+
throw 'Input too large, results in inaccurate fibonacci value.';
24+
}
2225
var n1 = 0;
2326
var n2 = 1;
2427
var aux;

test/others/fibonacci.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
var mod = require('../../src/others/fibonacci.js');
4+
var fibonacci = mod.fibonacci;
5+
6+
describe('fibonacci algorithm', function () {
7+
it('should return value 1 with input 1.', function () {
8+
expect(fibonacci(1)).toBe(1);
9+
});
10+
it('should return value 1 with input 2.', function () {
11+
expect(fibonacci(2)).toBe(1);
12+
});
13+
it('should return value 2 with input 3.', function () {
14+
expect(fibonacci(3)).toBe(2);
15+
});
16+
it('should return value 3 with input 4.', function () {
17+
expect(fibonacci(4)).toBe(3);
18+
});
19+
it('should return value 5 with input 5.', function () {
20+
expect(fibonacci(5)).toBe(5);
21+
});
22+
it('should be 83621143489848422977 with input 97.', function () {
23+
expect(fibonacci(97)).toBe(83621143489848422977);
24+
});
25+
it('should throw when input is too large.', function () {
26+
expect(function () {fibonacci(98)}).toThrow('Input too large, results in inaccurate fibonacci value.');
27+
});
28+
});

0 commit comments

Comments
 (0)