From bbbc6d6d32352e3557999e73db32a684a1f1f9bc Mon Sep 17 00:00:00 2001 From: Kamila Lambert Date: Mon, 8 May 2017 15:28:23 -0700 Subject: [PATCH] all fibonacci tests passing --- src/fibonacci.js | 18 ++++++++++++++++++ test/fibonacci_test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/fibonacci.js create mode 100644 test/fibonacci_test.js diff --git a/src/fibonacci.js b/src/fibonacci.js new file mode 100644 index 0000000..688ea63 --- /dev/null +++ b/src/fibonacci.js @@ -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 +} diff --git a/test/fibonacci_test.js b/test/fibonacci_test.js new file mode 100644 index 0000000..b2c1387 --- /dev/null +++ b/test/fibonacci_test.js @@ -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); + }); +});