-1

I understand the logic of recursion a function calls a function with base case then terminate, what I have here is a code which logs a simple recursion, what I don't get is it started logging with the condition reached, condition met: 0?

function factorialize(num) {

    if(num === 0){
        console.log('condition met: '+num);
        return 1;
    }

    var x = factorialize(num-1); // iterate
    var toReturn = num*x;

    console.log("Current call: num = " + num
            + " x = " + x
            + "\n"
            + "Returning " + toReturn
            );

    return toReturn;

}

factorialize(5);

I was expecting this code to log the output first and lastly the condition is reached?

output and demo output

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • *to understand recursion, you need to first understand recursion* **chuckles** –  Aug 14 '16 at 21:30
  • why did it log the base case first? @vaxquis – learningjavascriptks Aug 14 '16 at 21:31
  • 1
    because `factorialize(5)` calls `factorialize(4)`, which calls `factorialize(3)`, which calls `factorialize(2)`, which calls `factorialize(1)`, which calls `factorialize(0)`? –  Aug 14 '16 at 21:34
  • You should be able to "execute" this code on paper to understand how it works. Execution hits that `console.log` first. – Blorgbeard Aug 14 '16 at 21:34
  • @vaxquis got it, is there a way that I can see the call stack on debugger or how can I log them? – learningjavascriptks Aug 14 '16 at 22:20
  • @learningjavascriptks http://stackoverflow.com/questions/280389/how-do-you-find-out-the-caller-function-in-javascript http://stackoverflow.com/questions/4671031/print-function-log-stack-trace-for-entire-program-using-firebug –  Aug 15 '16 at 00:03

2 Answers2

1

In recursion, first you go "in", then you come "out".

Anything before the recursive factorialize call is on the way "in".

Anything after it is on the way "out".

Since your second console.log statement comes after the recursive call, it executes on the way "out".

Your first console.log statement is inside an if statement. The if statement comes before the recursive call, so it is tested at every level on the way "in", but it only does the console.log statement when it arrives at the bottom of the well.

Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
0

As already pointed out, it's

because factorialize(5) calls factorialize(4), which calls factorialize(3), which calls factorialize(2), which calls factorialize(1), which calls factorialize(0)

And then all the logs occurs.

Community
  • 1
  • 1
Yitzchak
  • 3,303
  • 3
  • 30
  • 50
  • can you please elaborate more about the call stack? I was confused with my output, at the very bottom, it logs returning 120. so it returned 120, but why is the condition met at the very first line on the log? – learningjavascriptks Aug 14 '16 at 21:45
  • When the code starts it enters the function the first time. Check your if which returns false and pauses execution of the function to enter it second time... And so on, until the last call prints your "condition met" and it ends the function and continues from the last function call to finish it too, so it prints the next log, and again continue the last function call and prints again until execution terminates – Yitzchak Aug 14 '16 at 21:50
  • 1
    @vaxquis thanks for the edit – Yitzchak Aug 14 '16 at 21:53
  • @Yitschak n/p, mate. –  Aug 14 '16 at 22:02