Skip to content

Commit 15f1b82

Browse files
authored
Merge pull request #370 from cats256/patch-2
Update fibonacci-solution.js
2 parents f164d79 + 51572a0 commit 15f1b82

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
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];
2+
if (count < 0) return "OOPS";
3+
if (count === 0) return 0;
4+
5+
let firstPrev = 1;
6+
let secondPrev = 0;
7+
8+
for (let i = 2; i <= count; i++) {
9+
let current = firstPrev + secondPrev;
10+
secondPrev = firstPrev;
11+
firstPrev = current;
12+
}
13+
14+
return firstPrev;
815
};
916

1017
module.exports = fibonacci;

0 commit comments

Comments
 (0)