Merge pull request #396 from jamienorthman/main
10_fibonacci: handling of string zero & update of tests
This commit is contained in:
commit
b8b1ae4eda
|
@ -16,9 +16,15 @@ describe('fibonacci', () => {
|
||||||
test.skip('25th fibonacci number is 75025', () => {
|
test.skip('25th fibonacci number is 75025', () => {
|
||||||
expect(fibonacci(25)).toBe(75025);
|
expect(fibonacci(25)).toBe(75025);
|
||||||
});
|
});
|
||||||
|
test.skip('0th fibonacci number is 0', () => {
|
||||||
|
expect(fibonacci(0)).toBe(0);
|
||||||
|
});
|
||||||
test.skip('doesn\'t accept negatives', () => {
|
test.skip('doesn\'t accept negatives', () => {
|
||||||
expect(fibonacci(-25)).toBe("OOPS");
|
expect(fibonacci(-25)).toBe("OOPS");
|
||||||
});
|
});
|
||||||
|
test.skip('DOES accept strings', () => {
|
||||||
|
expect(fibonacci("0")).toBe(0);
|
||||||
|
});
|
||||||
test.skip('DOES accept strings', () => {
|
test.skip('DOES accept strings', () => {
|
||||||
expect(fibonacci("1")).toBe(1);
|
expect(fibonacci("1")).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,15 @@
|
||||||
const fibonacci = function(count) {
|
const fibonacci = function(countArg) {
|
||||||
|
// checks argument's type and makes sure we use
|
||||||
|
// a number throughout rest of function.
|
||||||
|
let count
|
||||||
|
if (typeof countArg !== 'number') {
|
||||||
|
count = parseInt(countArg)
|
||||||
|
} else {
|
||||||
|
count = countArg
|
||||||
|
}
|
||||||
|
|
||||||
if (count < 0) return "OOPS";
|
if (count < 0) return "OOPS";
|
||||||
if (count === 0) return 0;
|
if (count == 0) return 0;
|
||||||
|
|
||||||
let firstPrev = 1;
|
let firstPrev = 1;
|
||||||
let secondPrev = 0;
|
let secondPrev = 0;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
const fibonacci = require('./fibonacci-solution');
|
const fibonacci = require('./fibonacci-solution')
|
||||||
|
|
||||||
describe('fibonacci', () => {
|
describe('fibonacci', () => {
|
||||||
test('4th fibonacci number is 3', () => {
|
test('4th fibonacci number is 3', () => {
|
||||||
|
@ -16,19 +16,22 @@ describe('fibonacci', () => {
|
||||||
test('25th fibonacci number is 75025', () => {
|
test('25th fibonacci number is 75025', () => {
|
||||||
expect(fibonacci(25)).toBe(75025);
|
expect(fibonacci(25)).toBe(75025);
|
||||||
});
|
});
|
||||||
test('0th fibonacci number is o', () => {
|
test('0th fibonacci number is 0', () => {
|
||||||
expect(fibonacci(0)).toBe(0);
|
expect(fibonacci(0)).toBe(0);
|
||||||
});
|
});
|
||||||
test("doesn't accept negatives", () => {
|
test('doesn\'t accept negatives', () => {
|
||||||
expect(fibonacci(-25)).toBe('OOPS');
|
expect(fibonacci(-25)).toBe("OOPS");
|
||||||
});
|
});
|
||||||
test('DOES accept strings', () => {
|
test('DOES accept strings', () => {
|
||||||
expect(fibonacci('1')).toBe(1);
|
expect(fibonacci("0")).toBe(0);
|
||||||
});
|
});
|
||||||
test('DOES accept strings', () => {
|
test('DOES accept strings', () => {
|
||||||
expect(fibonacci('2')).toBe(1);
|
expect(fibonacci("1")).toBe(1);
|
||||||
});
|
});
|
||||||
test('DOES accept strings', () => {
|
test('DOES accept strings', () => {
|
||||||
expect(fibonacci('8')).toBe(21);
|
expect(fibonacci("2")).toBe(1);
|
||||||
|
});
|
||||||
|
test('DOES accept strings', () => {
|
||||||
|
expect(fibonacci("8")).toBe(21);
|
||||||
});
|
});
|
||||||
});
|
});
|
Loading…
Reference in New Issue