diff --git a/foundations/object_basics/03_fibonacci/README.md b/foundations/object_basics/03_fibonacci/README.md index 77b62de3348..35053046c39 100644 --- a/foundations/object_basics/03_fibonacci/README.md +++ b/foundations/object_basics/03_fibonacci/README.md @@ -2,11 +2,11 @@ Create a function that returns a specific member of the Fibonacci sequence (series of numbers in which each number is the sum of the two preceding numbers). To learn more about Fibonacci sequences, go to: https://en.wikipedia.org/wiki/Fibonacci_sequence -In this exercise, the Fibonacci sequence used is 1, 1, 2, 3, 5, 8, etc. (i.e. starting at 1, not 0). +In this exercise, the Fibonacci sequence used is 1, 1, 2, 3, 5, 8, etc. (i.e. starting at 1, not 0): ```javascript fibonacci(4); // returns the 4th member of the series: 3 (1, 1, 2, 3) fibonacci(6); // returns 8 ``` -If given a negative number, the function should return `"OOPS"`. \ No newline at end of file +If given a negative number, `NaN`, or not a number type at all, the function should return `"OOPS"`, i.e. only proceed with positive numbers. diff --git a/foundations/object_basics/03_fibonacci/fibonacci.spec.js b/foundations/object_basics/03_fibonacci/fibonacci.spec.js index de632d833f2..988337ac0d1 100644 --- a/foundations/object_basics/03_fibonacci/fibonacci.spec.js +++ b/foundations/object_basics/03_fibonacci/fibonacci.spec.js @@ -1,4 +1,4 @@ -const fibonacci = require('./fibonacci') +const fibonacci = require('./fibonacci-solution') describe('fibonacci', () => { test('4th fibonacci number is 3', () => { @@ -22,16 +22,12 @@ describe('fibonacci', () => { test.skip('doesn\'t accept negatives', () => { expect(fibonacci(-25)).toBe("OOPS"); }); - test.skip('DOES accept strings', () => { - expect(fibonacci("0")).toBe(0); + test.skip('doesn\'t accept NaN', () => { + expect(fibonacci(NaN)).toBe("OOPS"); }); - test.skip('DOES accept strings', () => { - expect(fibonacci("1")).toBe(1); + test.skip('doesn\'t accept non-numbers types', () => { + expect(fibonacci("0")).toBe("OOPS"); + expect(fibonacci([])).toBe("OOPS"); + expect(fibonacci({})).toBe("OOPS"); }); - test.skip('DOES accept strings', () => { - expect(fibonacci("2")).toBe(1); - }); - test.skip('DOES accept strings', () => { - expect(fibonacci("8")).toBe(21); - }); -}); +}) diff --git a/foundations/object_basics/03_fibonacci/solution/fibonacci-solution.js b/foundations/object_basics/03_fibonacci/solution/fibonacci-solution.js index 1327ce92c5e..481fc37b9f5 100644 --- a/foundations/object_basics/03_fibonacci/solution/fibonacci-solution.js +++ b/foundations/object_basics/03_fibonacci/solution/fibonacci-solution.js @@ -1,15 +1,11 @@ -const fibonacci = function(countArg) { +const fibonacci = function(count) { // 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 (typeof count !== 'number' || count < 0 || Number.isNaN(count)) { + return "OOPS"; } - - if (count < 0) return "OOPS"; - if (count == 0) return 0; + + if (count === 0) return 0; let firstPrev = 1; let secondPrev = 0; @@ -21,7 +17,6 @@ const fibonacci = function(countArg) { } return firstPrev; - }; // Another way to do it is by using an iterative approach with an array containing two values, 0 and 1. diff --git a/foundations/object_basics/03_fibonacci/solution/fibonacci-solution.spec.js b/foundations/object_basics/03_fibonacci/solution/fibonacci-solution.spec.js index fdb0badfac6..1858deb5b34 100644 --- a/foundations/object_basics/03_fibonacci/solution/fibonacci-solution.spec.js +++ b/foundations/object_basics/03_fibonacci/solution/fibonacci-solution.spec.js @@ -22,16 +22,28 @@ describe('fibonacci', () => { test('doesn\'t accept negatives', () => { expect(fibonacci(-25)).toBe("OOPS"); }); - test('DOES accept strings', () => { - expect(fibonacci("0")).toBe(0); + test('doesn\'t accept strings', () => { + expect(fibonacci("0")).toBe("OOPS"); }); - test('DOES accept strings', () => { - expect(fibonacci("1")).toBe(1); + test('doesn\'t accept NaN', () => { + expect(fibonacci(NaN)).toBe("OOPS"); }); - test('DOES accept strings', () => { - expect(fibonacci("2")).toBe(1); + test('doesn\'t accept undefined', () => { + expect(fibonacci(undefined)).toBe("OOPS"); }); - test('DOES accept strings', () => { - expect(fibonacci("8")).toBe(21); + test('doesn\'t accept null', () => { + expect(fibonacci(null)).toBe("OOPS"); }); -}); \ No newline at end of file + test('doesn\'t accept Boolean', () => { + expect(fibonacci(true)).toBe("OOPS"); + }); + test('doesn\'t accept Boolean', () => { + expect(fibonacci(false)).toBe("OOPS"); + }); + test('doesn\'t accept Array', () => { + expect(fibonacci([1])).toBe("OOPS"); + }); + test('doesn\'t accept Object', () => { + expect(fibonacci({num: 1})).toBe("OOPS"); + }); +});