Skip to content
Open
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
4 changes: 2 additions & 2 deletions foundations/object_basics/03_fibonacci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"`.
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.
20 changes: 8 additions & 12 deletions foundations/object_basics/03_fibonacci/fibonacci.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fibonacci = require('./fibonacci')
const fibonacci = require('./fibonacci-solution')

describe('fibonacci', () => {
test('4th fibonacci number is 3', () => {
Expand All @@ -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);
});
});
})
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
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");
});
});