有逻辑或语法问题的for循环
A for loop with a logic or a syntax issue
所以我试图将数组的长度作为字符串推送到另一个数组上。我的逻辑是。。。对于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>' );
};
相关文章:
- 为什么JavaScript在for循环为3时向所有4发出警报
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 我的javascript for循环不起作用
- For循环冻结Javascript
- 如何在for循环中添加事件侦听器
- 双“for”循环(循环)
- javascript for循环不起作用
- for循环中的javascript if语句找不到==
- Javascript在for循环中等待处理请求
- For循环在Jquery中只运行一次
- 如何在for循环中使用计数器
- for循环中的JavaScript闭包
- 为什么我们在ES2015中需要一个新的for循环结构,而我们已经有了for、forEach
- For循环在调用时未运行
- 如何使用for循环添加所有按钮'单击事件
- 如何更改在for循环中生成的圆的位置
- 为什么这个For循环会使浏览器实验室崩溃
- 为什么我使用javascript获得了一个无限的for循环
- 在for循环中使用多维数组设置google.maps.Marker图标
- 如何在angularJS中运行for循环而不使用html标记