0

I'm just playing with some JavaScript and have the following code:

function Stack() {
    // Wrapper class for Array. This class only exposes the push
    // and pop methods from the Array and the length property
    // to mimic the a LIFO Stack.

    // Instantiate new Array object.
    this.stack = new Array();

    // Pushes a new value on to the stack.
    // @param arg to be pushed.
    this.push = function(arg) {
        return this.stack.push(arg);
    }

    // Pops a value from the stack and returns it.
    this.pop = function() {
        return this.stack.pop();
    }

    // Get size of the Stack.
    this.size = function() {
        return this.stack.length;
    }
}

var stack = new Stack();

// Push 10 items on to the stack
for (var i = 0; i < 10; i++) {
    stack.push(i);
}

for (var i = 0; i < stack.size(); i++) {
    console.log(stack.pop());
}

The first part defines a Stack object that is really just a wrapper around the native Array object, but only exposes some methods and properties to make it like a LIFO stack. To test it I wrote the code at the bottom. However, when I try to use stack.size() to return the current size of the stack in the for loop the loop only iterates 5 times. Whereas if I assign the return value of that method call to a variable and pass the variable in to the for loop it iterates over the correct number of times (10). Why is this? Should I not be able to use stack.size() inside the for loop?

2
  • jsfiddle.net/TnKzH - not reproducible Commented Sep 16, 2013 at 11:11
  • 5
    Because every time you pop something from the stack, the size gets smaller? Commented Sep 16, 2013 at 11:11

5 Answers 5

1

because the stack.size() in the for loop is being tested after each execution so each time you pop an element from the stack, the size get smaller. while if you use the variable, you are saving the stack size in that variable and even when you pop from the stack that variable won't change.

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

1 Comment

Thanks I overlooked the fact that var i = 0; i < stack.size(); i++ is tested on each iteration. Silly mistake!
1

When you use stack.size() in the loop, after the loop iterates 5 times, stack.size() equals 5, since you have popped the stack for 5 times, and i also equals 5. In the next iteration, i is greater than stack's size, and the loop ends finally.

Comments

1

The alternative way to write it is:

for (var i = 0, l = stack.size(); i < l; i++) {
  console.log(stack.pop());
}

And the best way to write for...loops IMO.

2 Comments

Is the whole expression not evaluated on each iteration, or only the last two parts from i < l; i++)?
It's exactly the same as your version, just the variables are assigned in the loop construct rather than outside it.
0

For every iteration you will remove one of the objects and make the size of the array 1 less.

Comments

0

Also, why not just do:

this.pop = this.stack.pop;

rather than

this.pop = function() {
    return this.stack.pop();
}

seems like an unnecessary closure to me?

Comments

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.