Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
5 changes: 3 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,12 @@

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). The function should only accept number inputs and reject anything else.
Comment thread
Vairiation marked this conversation as resolved.
Resolved

```javascript
fibonacci(4); // returns the 4th member of the series: 3 (1, 1, 2, 3)
fibonacci(6); // returns 8
fibonacci("6"); // returns "OOPS"
Comment thread
Vairiation marked this conversation as resolved.
Resolved
```

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..
Comment thread
Vairiation marked this conversation as resolved.
Resolved
32 changes: 22 additions & 10 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,28 @@ 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 strings', () => {
expect(fibonacci("0")).toBe("OOPS");
});
test.skip('DOES accept strings', () => {
expect(fibonacci("1")).toBe(1);
test.skip('doesn\'t accept NaN', () => {
expect(fibonacci(NaN)).toBe("OOPS");
});
test.skip('DOES accept strings', () => {
expect(fibonacci("2")).toBe(1);
test.skip('doesn\'t accept undefined', () => {
expect(fibonacci(undefined)).toBe("OOPS");
});
test.skip('DOES accept strings', () => {
expect(fibonacci("8")).toBe(21);
test.skip('doesn\'t accept null', () => {
expect(fibonacci(null)).toBe("OOPS");
});
});
test.skip('doesn\'t accept Boolean', () => {
expect(fibonacci(true)).toBe("OOPS");
});
test.skip('doesn\'t accept Boolean', () => {
expect(fibonacci(false)).toBe("OOPS");
});
test.skip('doesn\'t accept Array', () => {
expect(fibonacci([1])).toBe("OOPS");
});
test.skip('doesn\'t accept Object', () => {
expect(fibonacci({num: 1})).toBe("OOPS");
});
})
Comment thread
Vairiation marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ 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 0;
if (typeof countArg !== 'number' || countArg < 0 || Number.isNaN(countArg)) {
return "OOPS";
} else count = countArg;

if (count === 0) return 0;
Comment thread
Vairiation marked this conversation as resolved.
Resolved

let firstPrev = 1;
let secondPrev = 0;
Expand All @@ -24,6 +21,8 @@ const fibonacci = function(countArg) {

Comment thread
Vairiation marked this conversation as resolved.
Resolved
};

console.log(fibonacci('hello'))

Comment thread
Vairiation marked this conversation as resolved.
Resolved
// Another way to do it is by using an iterative approach with an array containing two values, 0 and 1.
// const fib = [0, 1];
// for (let i = 2; i <= count; i++) {
Expand All @@ -32,3 +31,4 @@ const fibonacci = function(countArg) {
// return fib[count];

module.exports = fibonacci;

Comment thread
Vairiation marked this conversation as resolved.
Resolved
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");
});
});