8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 908c4ed commit 123e00dCopy full SHA for 123e00d
10_fibonacci/solution/fibonacci-solution.js
@@ -1,12 +1,25 @@
1
const fibonacci = function(count) {
2
if (count < 0) return "OOPS";
3
if (count === 0) return 0;
4
-
5
- const fib = [0, 1];
+
+ let firstPrev = 1;
6
+ let secondPrev = 0;
7
8
for (let i = 2; i <= count; i++) {
- fib[i] = fib[i - 1] + fib[i - 2];
9
+ let current = firstPrev + secondPrev;
10
+ secondPrev = firstPrev;
11
+ firstPrev = current;
12
}
- return fib[count];
13
14
+ return firstPrev;
15
16
};
17
18
+// Another way to do it is by using an iterative approach with an array containing two values, 0 and 1.
19
+// const fib = [0, 1];
20
+// for (let i = 2; i <= count; i++) {
21
+// fib[i] = fib[i - 1] + fib[i - 2];
22
+// }
23
+// return fib[count];
24
25
module.exports = fibonacci;
0 commit comments