Skip to content

Commit b95ffc0

Browse files
committed
Add fibonacci itterative implementation
1 parent ac991d9 commit b95ffc0

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

JavaScript/8-fib-iter.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const fibonacci1 = n => {
4+
if (n <= 2) return 1;
5+
return fibonacci1(n - 1) + fibonacci1(n - 2);
6+
};
7+
8+
console.log(fibonacci1(10));
9+
10+
const fibonacci2 = n => {
11+
let a = 1;
12+
let b = 0;
13+
let c = 0;
14+
let counter = n;
15+
while (n > 0) {
16+
c = a + b;
17+
b = a;
18+
a = c;
19+
n--;
20+
}
21+
return b;
22+
};
23+
24+
console.log(fibonacci2(10));

0 commit comments

Comments
 (0)