0
var stack = new Array();
var ii = 0;

function pushTutor(item) {
var jj = stack.length;
    for(ii=0;ii<jj;ii++) {
        stack.push(item);
        alert(stack);
    }
}

I remember the stack.length cause the issue which is not able to loop at all. What is the solution for this?

4
  • 1
    What do you want to do? Why the loop? Commented Apr 5, 2011 at 9:34
  • What's the problem for which's solution you're asking? Commented Apr 5, 2011 at 9:36
  • this is strange indeed : for each element in stack you add the same item, stack = [a,b,c] pushTutor(d) will give [a,b,c,d,d,d] I guess. Commented Apr 5, 2011 at 9:37
  • I assume that your goal is to insert the item in the end of the array? So push does exactly this, no need for loop at all. Commented Apr 5, 2011 at 10:28

3 Answers 3

2

Well, aside from the fact that you don't need a for loop for what you're trying to achieve here, stack has no items, so its length is 0. As such, your loop will never execute.

If you just want to push the item, surely it'd be better to do:

function pushTutor(item)
{
    stack.push(item);
    alert(stack.length);
    // Alerting stack here would simply alert 'array'
}
Sign up to request clarification or add additional context in comments.

1 Comment

After you push, it should alert item and then item,item next time
0

The code does not make sense.

Perhaps you want

var stack = new Array();

function pushTutor(item) {
  stack.push(item);
  alert(stack);
}

Comments

0

stack is empty, that's a problem.

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.