开发者

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?


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


The code does not make sense.

Perhaps you want

var stack = new Array();

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


stack is empty, that's a problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜