Update Fibonacci solution, test to focus on arrays

Previous Fibonacci solution didn't use arrays, and since this is an
array-heavy section, it seemed better to have that be the method here.

Also, it accounts for entering 0 as an argument without having to add
any extra code, which takes care of some currently open issues.
Issues #192 and #236 are about the same thing.

I added a test for that as well.
This commit is contained in:
MarLatte 2023-05-20 02:22:12 -04:00
parent 3e530e3f61
commit 3256f980b0
2 changed files with 10 additions and 11 deletions

View file

@ -1,14 +1,10 @@
const fibonacci = function (count) { const fibonacci = function(count) {
if (count < 0) return "OOPS"; if (count < 0) return "OOPS"
if (count === 0) return 0; const fibPart = [0, 1];
let a = 0; for (let index = 1; index < count; index++) {
let b = 1; fibPart.push(fibPart[index] + fibPart[index -1]);
for (let i = 1; i < count; i++) {
const temp = b;
b = a + b;
a = temp;
} }
return b; return fibPart[count];
}; };
module.exports = fibonacci; module.exports = fibonacci;

View file

@ -16,6 +16,9 @@ describe('fibonacci', () => {
test('25th fibonacci number is 75025', () => { test('25th fibonacci number is 75025', () => {
expect(fibonacci(25)).toBe(75025); expect(fibonacci(25)).toBe(75025);
}); });
test('0th fibonacci number is o', () => {
expect(fibonacci(0)).toBe(0);
});
test("doesn't accept negatives", () => { test("doesn't accept negatives", () => {
expect(fibonacci(-25)).toBe('OOPS'); expect(fibonacci(-25)).toBe('OOPS');
}); });