Write an iterative method to generate nth Fibonacci number.
F(n) = F(n-1) + F(n-1); F(0) == 0, F(1) == 1;
function fibonacci(n) { //your code here } console.log( fibonacci(99) );//from w ww .j a va 2 s. c om /* Output: ($ /usr/bin/node 011.js) 218922995834555200000 */
function fibonacci(n) { if (n === 0) { return 0; } if (n === 1) { return 1; } if (n < 1) { return -1; } var i = 0; var c = 0; var a = 1; var b = 1; for (i = 3; i <= n; i++) { c = a + b; a = b; b = c; } return b; } console.log( fibonacci(99) ); /* Output: ($ /usr/bin/node 011.js) 218922995834555200000 */