From f2c0d0955b83a56e4ebb737765b09ca129407185 Mon Sep 17 00:00:00 2001 From: Miko <100534556+jamienorthman@users.noreply.github.com> Date: Mon, 18 Sep 2023 23:01:33 +0200 Subject: [PATCH 01/10] Added handling of string case for 0 --- 10_fibonacci/solution/fibonacci-solution.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/10_fibonacci/solution/fibonacci-solution.js b/10_fibonacci/solution/fibonacci-solution.js index acdd1e0..a002ffe 100644 --- a/10_fibonacci/solution/fibonacci-solution.js +++ b/10_fibonacci/solution/fibonacci-solution.js @@ -1,6 +1,6 @@ const fibonacci = function(count) { if (count < 0) return "OOPS"; - if (count === 0) return 0; + if (count == 0) return 0; let firstPrev = 1; let secondPrev = 0; From 5ac1931f72c1b04ff1bd30ddc653280e43d130e6 Mon Sep 17 00:00:00 2001 From: Miko <100534556+jamienorthman@users.noreply.github.com> Date: Mon, 18 Sep 2023 23:06:29 +0200 Subject: [PATCH 02/10] Added test cases for 0 and string 0 --- 10_fibonacci/fibonacci.spec.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/10_fibonacci/fibonacci.spec.js b/10_fibonacci/fibonacci.spec.js index 7f62213..7617791 100644 --- a/10_fibonacci/fibonacci.spec.js +++ b/10_fibonacci/fibonacci.spec.js @@ -16,9 +16,15 @@ describe('fibonacci', () => { test.skip('25th fibonacci number is 75025', () => { expect(fibonacci(25)).toBe(75025); }); + test('0th fibonacci number is o', () => { + expect(fibonacci(0)).toBe(0); + }); test.skip('doesn\'t accept negatives', () => { expect(fibonacci(-25)).toBe("OOPS"); }); + test.skip('DOES accept strings', () => { + expect(fibonacci("0")).toBe(0); + }); test.skip('DOES accept strings', () => { expect(fibonacci("1")).toBe(1); }); From 59a2f1ce4796c905f3e680c66ffbfcc04de20e46 Mon Sep 17 00:00:00 2001 From: Miko <100534556+jamienorthman@users.noreply.github.com> Date: Mon, 18 Sep 2023 23:06:51 +0200 Subject: [PATCH 03/10] Added test case for string 0 --- .../solution/fibonacci-solution.spec.js | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/10_fibonacci/solution/fibonacci-solution.spec.js b/10_fibonacci/solution/fibonacci-solution.spec.js index 4e6a44d..7617791 100644 --- a/10_fibonacci/solution/fibonacci-solution.spec.js +++ b/10_fibonacci/solution/fibonacci-solution.spec.js @@ -1,34 +1,37 @@ -const fibonacci = require('./fibonacci-solution'); +const fibonacci = require('./fibonacci') describe('fibonacci', () => { test('4th fibonacci number is 3', () => { expect(fibonacci(4)).toBe(3); }); - test('6th fibonacci number is 8', () => { + test.skip('6th fibonacci number is 8', () => { expect(fibonacci(6)).toBe(8); }); - test('10th fibonacci number is 55', () => { + test.skip('10th fibonacci number is 55', () => { expect(fibonacci(10)).toBe(55); }); - test('15th fibonacci number is 610', () => { + test.skip('15th fibonacci number is 610', () => { expect(fibonacci(15)).toBe(610); }); - test('25th fibonacci number is 75025', () => { + test.skip('25th fibonacci number is 75025', () => { expect(fibonacci(25)).toBe(75025); }); test('0th fibonacci number is o', () => { expect(fibonacci(0)).toBe(0); }); - test("doesn't accept negatives", () => { - expect(fibonacci(-25)).toBe('OOPS'); + test.skip('doesn\'t accept negatives', () => { + expect(fibonacci(-25)).toBe("OOPS"); }); - test('DOES accept strings', () => { - expect(fibonacci('1')).toBe(1); + test.skip('DOES accept strings', () => { + expect(fibonacci("0")).toBe(0); }); - test('DOES accept strings', () => { - expect(fibonacci('2')).toBe(1); + test.skip('DOES accept strings', () => { + expect(fibonacci("1")).toBe(1); }); - test('DOES accept strings', () => { - expect(fibonacci('8')).toBe(21); + test.skip('DOES accept strings', () => { + expect(fibonacci("2")).toBe(1); + }); + test.skip('DOES accept strings', () => { + expect(fibonacci("8")).toBe(21); }); }); From 3b84151724a57466fd88b752dee7514ead739644 Mon Sep 17 00:00:00 2001 From: Miko <100534556+jamienorthman@users.noreply.github.com> Date: Mon, 18 Sep 2023 23:11:09 +0200 Subject: [PATCH 04/10] Fixed usage of test (no skip) in solution --- .../solution/fibonacci-solution.spec.js | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/10_fibonacci/solution/fibonacci-solution.spec.js b/10_fibonacci/solution/fibonacci-solution.spec.js index 7617791..3bbaa61 100644 --- a/10_fibonacci/solution/fibonacci-solution.spec.js +++ b/10_fibonacci/solution/fibonacci-solution.spec.js @@ -1,37 +1,37 @@ -const fibonacci = require('./fibonacci') +const fibonacci = require('./fibonacci-solution') describe('fibonacci', () => { test('4th fibonacci number is 3', () => { expect(fibonacci(4)).toBe(3); }); - test.skip('6th fibonacci number is 8', () => { + test('6th fibonacci number is 8', () => { expect(fibonacci(6)).toBe(8); }); - test.skip('10th fibonacci number is 55', () => { + test('10th fibonacci number is 55', () => { expect(fibonacci(10)).toBe(55); }); - test.skip('15th fibonacci number is 610', () => { + test('15th fibonacci number is 610', () => { expect(fibonacci(15)).toBe(610); }); - test.skip('25th fibonacci number is 75025', () => { + test('25th fibonacci number is 75025', () => { expect(fibonacci(25)).toBe(75025); }); test('0th fibonacci number is o', () => { expect(fibonacci(0)).toBe(0); }); - test.skip('doesn\'t accept negatives', () => { + test('doesn\'t accept negatives', () => { expect(fibonacci(-25)).toBe("OOPS"); }); - test.skip('DOES accept strings', () => { + test('DOES accept strings', () => { expect(fibonacci("0")).toBe(0); }); - test.skip('DOES accept strings', () => { + test('DOES accept strings', () => { expect(fibonacci("1")).toBe(1); }); - test.skip('DOES accept strings', () => { + test('DOES accept strings', () => { expect(fibonacci("2")).toBe(1); }); - test.skip('DOES accept strings', () => { + test('DOES accept strings', () => { expect(fibonacci("8")).toBe(21); }); }); From e5c0f77d21b72996fd1535a1aa7fa5b7ce1ecd3f Mon Sep 17 00:00:00 2001 From: Miko <100534556+jamienorthman@users.noreply.github.com> Date: Mon, 18 Sep 2023 23:11:41 +0200 Subject: [PATCH 05/10] Fixed usage of test (skip all but first) in exercise --- 10_fibonacci/fibonacci.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/10_fibonacci/fibonacci.spec.js b/10_fibonacci/fibonacci.spec.js index 7617791..6bf1fd9 100644 --- a/10_fibonacci/fibonacci.spec.js +++ b/10_fibonacci/fibonacci.spec.js @@ -16,7 +16,7 @@ describe('fibonacci', () => { test.skip('25th fibonacci number is 75025', () => { expect(fibonacci(25)).toBe(75025); }); - test('0th fibonacci number is o', () => { + test.skip('0th fibonacci number is o', () => { expect(fibonacci(0)).toBe(0); }); test.skip('doesn\'t accept negatives', () => { From 38f0da96437874b26ad223c9ebc75b59a780ed18 Mon Sep 17 00:00:00 2001 From: Miko <100534556+jamienorthman@users.noreply.github.com> Date: Sun, 29 Oct 2023 11:51:42 +0100 Subject: [PATCH 06/10] More consistent spelling in 10_fibonacci Co-authored-by: Alex Younger <61510135+fortypercenttitanium@users.noreply.github.com> --- 10_fibonacci/solution/fibonacci-solution.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/10_fibonacci/solution/fibonacci-solution.spec.js b/10_fibonacci/solution/fibonacci-solution.spec.js index 3bbaa61..33023e8 100644 --- a/10_fibonacci/solution/fibonacci-solution.spec.js +++ b/10_fibonacci/solution/fibonacci-solution.spec.js @@ -16,7 +16,7 @@ describe('fibonacci', () => { test('25th fibonacci number is 75025', () => { expect(fibonacci(25)).toBe(75025); }); - test('0th fibonacci number is o', () => { + test('0th fibonacci number is 0', () => { expect(fibonacci(0)).toBe(0); }); test('doesn\'t accept negatives', () => { From 4a03e410bf7fb4fdd23d2f3180f58fbd006bafe8 Mon Sep 17 00:00:00 2001 From: Miko <100534556+jamienorthman@users.noreply.github.com> Date: Sun, 29 Oct 2023 11:52:01 +0100 Subject: [PATCH 07/10] More consistent spelling in 10_fibonacci Co-authored-by: Alex Younger <61510135+fortypercenttitanium@users.noreply.github.com> --- 10_fibonacci/fibonacci.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/10_fibonacci/fibonacci.spec.js b/10_fibonacci/fibonacci.spec.js index 6bf1fd9..de632d8 100644 --- a/10_fibonacci/fibonacci.spec.js +++ b/10_fibonacci/fibonacci.spec.js @@ -16,7 +16,7 @@ describe('fibonacci', () => { test.skip('25th fibonacci number is 75025', () => { expect(fibonacci(25)).toBe(75025); }); - test.skip('0th fibonacci number is o', () => { + test.skip('0th fibonacci number is 0', () => { expect(fibonacci(0)).toBe(0); }); test.skip('doesn\'t accept negatives', () => { From 80ca665767a48a52f2a7c57510f384b89eec3b43 Mon Sep 17 00:00:00 2001 From: Jamienorthman Date: Sun, 29 Oct 2023 12:36:58 +0100 Subject: [PATCH 08/10] added explicit conversion to number for fibonacci solution --- 10_fibonacci/solution/fibonacci-solution.js | 13 +++++++++++-- 10_fibonacci/solution/fibonacci-solution.spec.js | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/10_fibonacci/solution/fibonacci-solution.js b/10_fibonacci/solution/fibonacci-solution.js index a002ffe..953a5e1 100644 --- a/10_fibonacci/solution/fibonacci-solution.js +++ b/10_fibonacci/solution/fibonacci-solution.js @@ -1,4 +1,13 @@ -const fibonacci = function(count) { +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; @@ -14,4 +23,4 @@ const fibonacci = function(count) { return firstPrev; }; -module.exports = fibonacci; +module.exports = fibonacci; \ No newline at end of file diff --git a/10_fibonacci/solution/fibonacci-solution.spec.js b/10_fibonacci/solution/fibonacci-solution.spec.js index 33023e8..fdb0bad 100644 --- a/10_fibonacci/solution/fibonacci-solution.spec.js +++ b/10_fibonacci/solution/fibonacci-solution.spec.js @@ -34,4 +34,4 @@ describe('fibonacci', () => { test('DOES accept strings', () => { expect(fibonacci("8")).toBe(21); }); -}); +}); \ No newline at end of file From 908c4ed26ea00142da5e1a2f2a2f8e58c721d0da Mon Sep 17 00:00:00 2001 From: Luis Leiva <103515231+Luislev@users.noreply.github.com> Date: Sun, 5 Nov 2023 23:12:41 -0500 Subject: [PATCH 09/10] Update fibonacci-solution.js --- 10_fibonacci/solution/fibonacci-solution.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/10_fibonacci/solution/fibonacci-solution.js b/10_fibonacci/solution/fibonacci-solution.js index acdd1e0..5e3ff40 100644 --- a/10_fibonacci/solution/fibonacci-solution.js +++ b/10_fibonacci/solution/fibonacci-solution.js @@ -1,17 +1,12 @@ const fibonacci = function(count) { if (count < 0) return "OOPS"; if (count === 0) return 0; - - let firstPrev = 1; - let secondPrev = 0; + const fib = [0, 1]; for (let i = 2; i <= count; i++) { - let current = firstPrev + secondPrev; - secondPrev = firstPrev; - firstPrev = current; + fib[i] = fib[i - 1] + fib[i - 2]; } - - return firstPrev; + return fib[count]; }; module.exports = fibonacci; From 123e00d9331a80312bd459cbadd663f684a28963 Mon Sep 17 00:00:00 2001 From: Luis Leiva <103515231+Luislev@users.noreply.github.com> Date: Tue, 7 Nov 2023 21:27:27 -0500 Subject: [PATCH 10/10] Add alternative solution in fibonacci-solution.js --- 10_fibonacci/solution/fibonacci-solution.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/10_fibonacci/solution/fibonacci-solution.js b/10_fibonacci/solution/fibonacci-solution.js index 5e3ff40..644edbb 100644 --- a/10_fibonacci/solution/fibonacci-solution.js +++ b/10_fibonacci/solution/fibonacci-solution.js @@ -1,12 +1,25 @@ const fibonacci = function(count) { if (count < 0) return "OOPS"; if (count === 0) return 0; - - const fib = [0, 1]; + + let firstPrev = 1; + let secondPrev = 0; + for (let i = 2; i <= count; i++) { - fib[i] = fib[i - 1] + fib[i - 2]; + let current = firstPrev + secondPrev; + secondPrev = firstPrev; + firstPrev = current; } - return fib[count]; + + return firstPrev; + }; +// 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++) { +// fib[i] = fib[i - 1] + fib[i - 2]; +// } +// return fib[count]; + module.exports = fibonacci;