2

I'm a beginner in Javascript. And while trying recursion by myself, i got some strange result using while loop. And just the right result using If statement.

Here's the code and the result :

var test = function f(n){
    while(n > 0){
        document.write(n);
        f(--n);     
    }
};

test(5);

And the result :

5432112113211211432112113211211

While using If statement

var test = function f(n){
    if(n > 0){
        document.write(n);
        f(--n);     
    }
};

test(5);

The result is :

54321

I can't really debug it in the while case. It gets me confused at some point to be honest.

6
  • Why don't you just use the if statement then? Commented Jul 10, 2012 at 23:25
  • I don't understand what is your problem. The first function works as expected, although I have to admit its very weird and not intuitive. Commented Jul 10, 2012 at 23:25
  • 1
    See stackoverflow.com/questions/3070693/… Commented Jul 10, 2012 at 23:27
  • I was just playing around with recursion. First i tried it with While and then If, i want to know why while loop gives this result ? Commented Jul 10, 2012 at 23:27
  • @missingno Thanks for the link. I see that's the same problem as me, but unfortunately can't debug the code here Commented Jul 10, 2012 at 23:38

2 Answers 2

2

5432112113211211432112113211211 You can see the pattern:

5432112113211211
 432112113211211

543211211
  3211211
 43211211
  3211211

543211
   211
  3211
   211
 43211
   211
  3211
   211

etc

These are just all the loops it goes through. First it prints 5, then splits of for 4 and splits again for 3 etc.

Sign up to request clarification or add additional context in comments.

4 Comments

Interesting, but may you debug the code that generates such pattern for me ? because i don't know how it goes at all. I mean after n = 0 what happens ?
If you call f(0) the first while loop doesn't even loop once (because 0>0 is false), so nothing happens.
Well, nothing happens inside that function, but the important thing to note is that once that function is completed, execution continues at the calling function, where the value of n is not necessarily the same.
Not quite. The value of n is declared in the function argument list so there no global leakage. Also, scalar values get passed-by-value not by-reference so if you call var n = 5; f(n); console.log(n); it will print 5, not 0;
1

The important thing to note is that every time you run the code inside of f, a new scope is created with a new variable called n. So, by recursively calling f, you are adding to scope chain. You keep adding to the scope chain until n > 0 is false. Once it is false, you start traversing back up the scope chain, where other versions of n exist. The process repeats until all n variables in all of the scopes become 0.

Notice the pattern that occurs when I add some spaces to your numbers below. I've added a space every time the code has to go up the scope chain. Every set of numbers represents the code going down the scope chain.

54321 1 21 1 321 1 21 1 4321 1 21 1 321 1 21 1

Here is a jsfiddle that should be helpful. It prints two numbers: the first one represents which scope you are in and the second one is the same number that you were printing in your code. Look at the first numbers and try to wrap your head around how a new number is created for each scope. Try to think about what the value of n should be when you return to that scope later in the program.

6 Comments

Ah, very nice explanation using jsfiddle, thanks! .. But i have a couple questions : 1) why does (5:1) do here? why is it even there ?
1) notice you get all the way back to the first scope on (0:2). Since n is still greater than 0, it calls f(--n) again, creating a new scope and passing a value of 1.
@Rafael 2) Yes, you could think of it like that. Just remember that it is a new function and any parameters or variables you declare with var are part of that functions scope. As soon as you leave that new function, execution continues at the calling function.
@Rafael 3) With the if statement, you still go down the scope chain, but when you come back up, there is no more code to execute after f(--n), so you leave the function and climb back up to the next function up the scope chain. With the while loop, once you come back up, the while condition (n > 0) is tested again and if true, you keep looping.
@Rafael you're welcome! And kudos to you for digging until you really understood what was going on! That will get you very far (if it hasn't already) as a programmer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.