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..8f2df2301e9 100644 --- a/foundations/object_basics/03_fibonacci/fibonacci.spec.js +++ b/foundations/object_basics/03_fibonacci/fibonacci.spec.js @@ -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..642c8cbc532 100644 --- a/foundations/object_basics/03_fibonacci/solution/fibonacci-solution.js +++ b/foundations/object_basics/03_fibonacci/solution/fibonacci-solution.js @@ -1,15 +1,13 @@ -const fibonacci = function(countArg) { - // checks argument's type and makes sure we use +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 0; } - - if (count < 0) return "OOPS"; - if (count == 0) return 0; let firstPrev = 1; let secondPrev = 0; @@ -21,7 +19,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..15338c71e63 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,12 @@ 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 NaN', () => { + expect(fibonacci(NaN)).toBe("OOPS"); }); - test('DOES accept strings', () => { - expect(fibonacci("1")).toBe(1); + test('doesn\'t accept non-numbers types', () => { + expect(fibonacci("0")).toBe("OOPS"); + expect(fibonacci([])).toBe("OOPS"); + expect(fibonacci({})).toBe("OOPS"); }); - test('DOES accept strings', () => { - expect(fibonacci("2")).toBe(1); - }); - test('DOES accept strings', () => { - expect(fibonacci("8")).toBe(21); - }); -}); \ No newline at end of file +})