Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/fibonacci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default function fibonacci(input) {

const fibArray = [0, 1]

if (!Number.isInteger(input) || input < 1) {
return undefined
} else if (input === 1) {
return [0]
} else if (input === 2) {
return fibArray
} else {
for (let i = 2; i < input; i++) {
let current = fibArray[i - 1] + fibArray[i - 2]
fibArray.push(current)
}
}
return fibArray
}
26 changes: 26 additions & 0 deletions test/fibonacci_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect } from 'chai';
import fibonacci from '../src/fibonacci';

describe.only('fibonacci()', () => {
it('should be a function', () => {
expect(fibonacci).to.be.a('function');
});

it('should output an array', () => {
expect(fibonacci(1)).to.be.a('array');
});

it('should give a proper array', () => {
expect(fibonacci(10)).to.deep.equal([0, 1, 1, 2, 3, 5, 8, 13, 21, 34]);
});

it('should not try to run the function with the values of empty things', () => {
expect(fibonacci({})).to.equal(undefined);
expect(fibonacci('')).to.equal(undefined);
expect(fibonacci([])).to.equal(undefined);
});

it('should not let you plug in negitive integers', () => {
expect(fibonacci(-5)).to.equal(undefined);
});
});