Skip to content

Commit a3992aa

Browse files
Merge pull request #409 from Luislev/main
10_fibonacci: Add alternative solution in fibonacci-solution.js
2 parents 157972d + 123e00d commit a3992aa

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

10_fibonacci/solution/fibonacci-solution.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@ const fibonacci = function(count) {
44

55
let firstPrev = 1;
66
let secondPrev = 0;
7-
7+
88
for (let i = 2; i <= count; i++) {
99
let current = firstPrev + secondPrev;
1010
secondPrev = firstPrev;
1111
firstPrev = current;
1212
}
1313

1414
return firstPrev;
15+
1516
};
1617

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+
1725
module.exports = fibonacci;

0 commit comments

Comments
 (0)