Skip to content

🐛 Fix: #378 Removing Redundant Condition #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/algorithms/math/fibonacci/__test__/fibonacci.test.js
Original file line number Diff line number Diff line change
@@ -12,5 +12,6 @@ describe('fibonacci', () => {
expect(fibonacci(8)).toEqual([1, 1, 2, 3, 5, 8, 13, 21]);
expect(fibonacci(9)).toEqual([1, 1, 2, 3, 5, 8, 13, 21, 34]);
expect(fibonacci(10)).toEqual([1, 1, 2, 3, 5, 8, 13, 21, 34, 55]);
expect(() => fibonacci(-2)).toThrowError(new Error('Cannot have values less than 1'));
});
});
Original file line number Diff line number Diff line change
@@ -21,5 +21,6 @@ describe('fibonacciNth', () => {
expect(fibonacciNth(75)).toBe(2111485077978050);
expect(fibonacciNth(80)).toBe(23416728348467685);
expect(fibonacciNth(90)).toBe(2880067194370816120);
expect(() => fibonacciNth(-2)).toThrowError(new Error('Cannot have values less than 1'));
});
});
5 changes: 3 additions & 2 deletions src/algorithms/math/fibonacci/fibonacci.js
Original file line number Diff line number Diff line change
@@ -10,8 +10,9 @@ export default function fibonacci(n) {
let currentValue = 1;
let previousValue = 0;

if (n === 1) {
return fibSequence;
// Throw error if index is not correct
if (n < 1) {
throw new Error('Cannot have values less than 1');
}

let iterationsCounter = n - 1;
5 changes: 3 additions & 2 deletions src/algorithms/math/fibonacci/fibonacciNth.js
Original file line number Diff line number Diff line change
@@ -8,8 +8,9 @@ export default function fibonacciNth(n) {
let currentValue = 1;
let previousValue = 0;

if (n === 1) {
return 1;
// Throw error if index is not correct
if (n < 1) {
throw new Error('Cannot have values less than 1');
}

let iterationsCounter = n - 1;