From 76551b0e8a8ce094df4413056151654e49596b8f Mon Sep 17 00:00:00 2001 From: cats256 <59489624+cats256@users.noreply.github.com> Date: Tue, 4 Jul 2023 12:07:04 -0500 Subject: [PATCH 1/3] Update fibonacci-solution.js Better O(n) time --- 10_fibonacci/solution/fibonacci-solution.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/10_fibonacci/solution/fibonacci-solution.js b/10_fibonacci/solution/fibonacci-solution.js index 010131c..e040ede 100644 --- a/10_fibonacci/solution/fibonacci-solution.js +++ b/10_fibonacci/solution/fibonacci-solution.js @@ -1,10 +1,17 @@ const fibonacci = function(count) { - if (count < 0) return "OOPS" - const fibPart = [0, 1]; - for (let index = 1; index < count; index++) { - fibPart.push(fibPart[index] + fibPart[index -1]); - } - return fibPart[count]; + if (count < 0) return "OOPS"; + if (count === 0) return 0; + + let first_prev = 1; + let second_prev = 0; + + for (let i = 2; i <= count; i++) { + let curr = first_prev + second_prev; + second_prev = first_prev; + first_prev = curr; + } + + return first_prev; }; module.exports = fibonacci; From fcb1c4971ac32995098b49613d5c944257fccb27 Mon Sep 17 00:00:00 2001 From: Nathan <59489624+cats256@users.noreply.github.com> Date: Wed, 5 Jul 2023 09:15:36 -0500 Subject: [PATCH 2/3] Update fibonacci-solution.js --- 10_fibonacci/solution/fibonacci-solution.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/10_fibonacci/solution/fibonacci-solution.js b/10_fibonacci/solution/fibonacci-solution.js index e040ede..1d7d9c6 100644 --- a/10_fibonacci/solution/fibonacci-solution.js +++ b/10_fibonacci/solution/fibonacci-solution.js @@ -2,16 +2,18 @@ const fibonacci = function(count) { if (count < 0) return "OOPS"; if (count === 0) return 0; - let first_prev = 1; - let second_prev = 0; + let firstPrev = 1; + let secondPrev = 0; + // For clarification: curr stands for current. This is standard syntax + for (let i = 2; i <= count; i++) { - let curr = first_prev + second_prev; - second_prev = first_prev; - first_prev = curr; + let curr = firstPrev + secondPrev; + secondPrev = firstPrev; + firstPrev = curr; } - return first_prev; + return firstPrev; }; module.exports = fibonacci; From 51572a070cf04619ea2259c0e6365480e1779989 Mon Sep 17 00:00:00 2001 From: Nathan <59489624+cats256@users.noreply.github.com> Date: Thu, 6 Jul 2023 09:47:20 -0500 Subject: [PATCH 3/3] Update fibonacci-solution.js --- 10_fibonacci/solution/fibonacci-solution.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/10_fibonacci/solution/fibonacci-solution.js b/10_fibonacci/solution/fibonacci-solution.js index 1d7d9c6..acdd1e0 100644 --- a/10_fibonacci/solution/fibonacci-solution.js +++ b/10_fibonacci/solution/fibonacci-solution.js @@ -4,13 +4,11 @@ const fibonacci = function(count) { let firstPrev = 1; let secondPrev = 0; - - // For clarification: curr stands for current. This is standard syntax for (let i = 2; i <= count; i++) { - let curr = firstPrev + secondPrev; + let current = firstPrev + secondPrev; secondPrev = firstPrev; - firstPrev = curr; + firstPrev = current; } return firstPrev;