Proyb2
Proyb2

Reputation: 993

for loop array length

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?

Upvotes: 0

Views: 1908

Answers (3)

BenM
BenM

Reputation: 53198

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'
}

Upvotes: 3

Headshota
Headshota

Reputation: 21449

stack is empty, that's a problem.

Upvotes: 0

mplungjan
mplungjan

Reputation: 177691

The code does not make sense.

Perhaps you want

var stack = new Array();

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

Upvotes: 1

Related Questions