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

A for loop with a logic or a syntax issue

本文关键字:for 循环 问题 语法      更新时间:2023-09-26

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

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

我想把x.length+1推到for循环中的y数组,这样它就变成了1.121231234

这就是我试图做到的,但从中读取

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));
    };

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

您正在编写y.push.toString

尝试:

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

但我认为你的意思是JSBIN演示:

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

也许您正在尝试执行此

    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);
}

这肯定是您想要的:

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());

以下是我如何获得

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