Different result for for-loop, while-loop and recursive method in a same file?

I calculated fibonacci series by for loop, while loop and through recursive method. I calculated through these 3 ways in a single file. I got result from for loop and while loop as usual. But when I added recusive method codes, I got the result as follows:

Codes:

// Calulating fibonacci series using for loop function fibonacci(n) {     var fibo = [0, 1];     for (var i = 2; i <= n; i++) {         fibo[i] = fibo[i - 1] + fibo[i - 2];     }     return (fibo); } var result = fibonacci(7); console.log(result)  // Calculating fibonacci series using while loop function fibonacci(n) {     var fibo = [0, 1];     var i = 2;     while (i <= n) {         fibo[i] = fibo[i - 1] + fibo[i - 2];         i++;     }     return fibo; } var result = fibonacci(7); console.log(result);  // Calculating fibonacci series in a recursive way function fibonacci(n) {     if (n == 0) {         return 0;     }     if (n == 1) {         return 1;     } else {         return fibonacci(n - 1) + fibonacci(n - 2);     } } var result = fibonacci(7); console.log(result); 

Result:

$ node fibonacci.js 13 13 13 

see the result from my recursive method is showing also for the for loop and while loop. When I commented out recursive method codes, the result showed:

$ node fibonacci.js [   0, 1, 1,  2,   3, 5, 8, 13  ] [   0, 1, 1,  2,   3, 5, 8, 13 ] 

as expected. Why is this happening??? This seems to be mysterious as I have not much enough knowledge about recursion.

Add Comment
2 Answer(s)

They’re all using the same name "fibonacci" and overwriting the definition for fibonacci. Only the last function declaration is being executed. Use different names.
(Function declarations get hoisted along with their definitions.)

Due to hoisting, the OP code is equivalent to:

var fibonacci = function fibonacci(n) { ...(forloop) } var fibonacci = function fibonacci(n) { ...(while) } var fibonacci = function fibonacci(n) { ...(recursive) }  var result = fibonacci(7); console.log(result);  var result = fibonacci(7); console.log(result);  var result = fibonacci(7); console.log(result); 

Just rename them so they don’t conflict.
Also added a recursive fib4 implementation that returns an array of fibonacci values.

// Calulating fibonacci series using for loop function fibonacci1(n) {     var fibo = [0, 1];     for (var i = 2; i <= n; i++) {         fibo[i] = fibo[i - 1] + fibo[i - 2];     }     return (fibo); } var result = fibonacci1(7); console.log(result)  // Calculating fibonacci series using while loop function fibonacci2(n) {     var fibo = [0, 1];     var i = 2;     while (i <= n) {         fibo[i] = fibo[i - 1] + fibo[i - 2];         i++;     }     return fibo; } var result = fibonacci2(7); console.log(result);  // Calculating fibonacci series in a recursive way function fibonacci3(n) {     if (n == 0) {         return 0;     }     if (n == 1) {         return 1;     } else {         return fibonacci3(n - 1) + fibonacci3(n - 2);     } } var result = fibonacci3(7); console.log(result);  // recursive with cache table 2nd parameter, returns cache table const fib4 = (n, fib=[0,1]) => {   fib[n] = fib[n] ?? (fib4(n-1, fib)[n-1] + fib4(n-2, fib)[n-2])   return fib } var result = fib4(7); console.log(result);

MDN article on hoisting

Add Comment
function fibonacci(n) {     var fibo = [0, 1];     for (var i = 2; i <= n; i++) {         fibo[i] = fibo[i - 1] + fibo[i - 2];     }     return fibo[fibo.length - 1]; } var result = fibonacci(7); console.log(result) 

If you run this example in chrome dev tools you will see the result will be always array. Because in for loop you are filling array, nothing wrong with the recursive method.

fibo[i] you are adding to array new elements each time. Try to use fibo[fibo.length - 1] and you will get the last element of your computation

Same with a while loop. You are a fill an array. If your goal to get a last value, not a whole list of computation you recursive variant is the only right one.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.