2

I played around with some recursive programming. I have one variable to keep track of my depth (d). The console log is for me to see where the program is at the moment.

var Test = function(){
	this.rekursiv = function(d){
		this.d = d;
		console.log("case 1 granted, d: " + this.d);		
		if( this.d < 3)	{
		console.log("going deeper..");
		this.rekursiv(this.d + 1);
		console.log("going back..");		
		}
		console.log("d: " + this.d  );
		}
}	
t = new Test();
t.rekursiv(0);

Here is my problem: Whenever I go one level deeper, I pass "this.d + 1" to the next level. Howevever, debugging the code (with the console.log) shows that d doesn't only get changed in one level/depth, but in every level/depth.

Why is this so? How can i prevent the code from doing this?

2
  • 3
    this.d isn't a variable. It's a property on an object, which happens to be the same object used as the value of this in the recursive call. There's no reason for a separate variable in your code, since you already have the d parameter. Commented Sep 4, 2016 at 17:53
  • You can write it like this: function Test(d) { if (d < 3) { console.log(d); Test(d + 1); console.log(d) } else console.log("base case") }. The problem with your function is, that it doesn't return anything. The base case just performs a side effect (console.log). Commented Sep 4, 2016 at 19:19

2 Answers 2

3

Why not use the local variable d?

With this.d, you are setting a property of the instance of Test. And with the ending of rekursiv, you do not change the value back to the former value.

var Test = function () {
        this.rekursiv = function(d) {
            console.log("case 1 granted, d: " + d);
            if (d < 3) {
                console.log("going deeper..");
                this.rekursiv(d + 1);
                console.log("going back..");		
            }
            console.log("d: " + d  );
        }
    },
    t = new Test;

t.rekursiv(0);

Another solution would be, to increase this.d at the start of the function rekursivand decrease it at the end.

var Test = function () {
        this.d = 0;
        this.rekursiv = function() {
            this.d++;
            console.log("case 1 granted, d: " + this.d);		
            if (this.d < 3)	{
                console.log("going deeper..");
                this.rekursiv();
                console.log("going back..");		
            }
            console.log("d: " + this.d  );
            this.d--;
        }
    },
    t = new Test;

    t.rekursiv();

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

Comments

2

Remove this line:

this.d = d;

Now, d is a local variable and no more instance variable.

var Test = function(){
  this.rekursiv = function(d){
    console.log("case 1 granted, d: " + d);
    if( d < 3)	{
      console.log("going deeper..");
      this.rekursiv(d + 1);
      console.log("going back..");
    }
    console.log("d: " + d  );
  }
}
t = new Test();
t.rekursiv(0);

2 Comments

Actually declaring d again inside rekursiv function is not necessary. This line can be removed.
@MichałMłoźniak You are right, it is pointless to redeclare the same variable.

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.