带有逻辑或语法问题的for循环

时间:2013-07-26 05:08:36

标签: javascript for-loop syntax logic

所以我试图将数组的长度作为字符串推送到另一个数组。 我的逻辑是这个......对于javascript

if    x = [1];
and   y = [1];

我希望将x.length + 1推送到for循环中的y数组,这样它就变成了 1 12 123 1234

这就是我试图做的事情,但是要读出

function push() { [native code] } function push() { [native code] } function push() { [native code] } function push() { [native code] }

这是我的代码

for (i=0; i < 100; i++) {

        var x = [1];
        var y = [1];

            document.writeln(y.push.toString(x.length + 1));

    };

这是逻辑错误还是语法错误?

4 个答案:

答案 0 :(得分:2)

你正在写y.push.toString

尝试:

for (i=0; i < 100; i++) {
    var x = [1];
    var y = [1];
    y.push(x.length + 1)
    document.writeln(y);
};

但我认为你的意思是JSBIN Demo

var y = [];    
for (i=0; i < 100; i++) {
    y.push(y.length + 1)
    document.writeln(y + '<br/>');
};

答案 1 :(得分:1)

也许你正在尝试这样做

    var x = [1];
    var y = [1];
for (i=0; i < 100; i++) {
       document.writeln(y.push(x.length + 1));
};

var x = [1];
var y = [1];
for (i=0; i < 100; i++) {
        y.push(y.length + 1)
        document.writeln(y);
    }

var y = ""
for (i=1; i < 100; i++) {
        y += i
        document.writeln(y);
}

答案 2 :(得分:1)

当然这就是你想要的:

var y = [1];
for (i = 0; i < 10; i++) {
    var x = y[y.length-1].toString() + (y.length + 1);
    y.push(x);
} 
document.writeln(y.toString());

答案 3 :(得分:0)

这是我如何得到它

var y = [1];
    document.writeln(y + '<br>');
for (i=2; i < 101; i++) {
    y.push(i);
    document.writeln(y + '<br>' );
};