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.
2 parents f164d79 + 51572a0 commit 15f1b82Copy full SHA for 15f1b82
10_fibonacci/solution/fibonacci-solution.js
@@ -1,10 +1,17 @@
1
const fibonacci = function(count) {
2
- if (count < 0) return "OOPS"
3
- const fibPart = [0, 1];
4
- for (let index = 1; index < count; index++) {
5
- fibPart.push(fibPart[index] + fibPart[index -1]);
6
- }
7
- return fibPart[count];
+ if (count < 0) return "OOPS";
+ if (count === 0) return 0;
+
+ let firstPrev = 1;
+ let secondPrev = 0;
8
+ for (let i = 2; i <= count; i++) {
9
+ let current = firstPrev + secondPrev;
10
+ secondPrev = firstPrev;
11
+ firstPrev = current;
12
+ }
13
14
+ return firstPrev;
15
};
16
17
module.exports = fibonacci;
0 commit comments