Skip to content

Commit 732b455

Browse files
authored
Merge pull request #287 from metacoder87/patch-24
Create FibonacciIterative.js
2 parents 3798540 + 7e85484 commit 732b455

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function fibonacciIterative(n) {
2+
if (n <= 1) {
3+
return n;
4+
}
5+
6+
let a = 0;
7+
let b = 1;
8+
let result;
9+
10+
for (let i = 2; i <= n; i++) {
11+
result = a + b;
12+
a = b;
13+
b = result;
14+
}
15+
16+
return result;
17+
}
18+
19+
function main() {
20+
console.log('Fibonacci (iterative) of 30:', fibonacciIterative(30));
21+
}
22+
23+
main();

0 commit comments

Comments
 (0)