为什么这段 JavaScript 代码不起作用?

发布于 2024-10-09 13:51:43 字数 351 浏览 1 评论 0原文

var arrayi = new Array();
for (var i=0; i<=9; i++)
{
    for (var o=0; o<=9; o++)
    {
        arrayi[i][o]=i + "" + o;
    }
}
for (var i = 0, j = 9; i <= 9; i++, j--)  
  document.write("arrayi[" + i + "][" + j + "]= " + arrayi[i][j]);

我试图将 00 分配给 arrayi[0][0],将 62 分配给 arrayi[6][2] 等,然后显示 [0][9],[1][8]...

var arrayi = new Array();
for (var i=0; i<=9; i++)
{
    for (var o=0; o<=9; o++)
    {
        arrayi[i][o]=i + "" + o;
    }
}
for (var i = 0, j = 9; i <= 9; i++, j--)  
  document.write("arrayi[" + i + "][" + j + "]= " + arrayi[i][j]);

I'm trying to assign 00 to arrayi[0][0], 62 to arrayi[6][2] etc.. and then display [0][9], [1][8]...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

夏日浅笑〃 2024-10-16 13:51:43

JavaScript 中的数组是一维的。

所以 arrayi[0]undefined,那么 arrayi[0][0] 就变成 undefined[0] ,显然不起作用。

您需要通过将数组分配给 arrayi 的索引来创建一个二维数组,所分配的实际值也存在错误:

var arrayi = []; // [] is the favored shorthand of new Array()
for (var i=0; i<=9; i++) { // always place { on the same line, 
                           // automatic semi colon insertion can screw you up in JavaScript

    arrayi[i] = []; // assign a new empty array
    for (var o=0; o<=9; o++) {
        arrayi[i][o]= i + '' + o; // i and o are both numbers so you will assign their sum
                                  // need to convert them to strings in order to concatenate them
    }
}

关于自动分号插入搞砸了,请看一下:

return  // js will insert a semi colon here, so this will return undefined
{ // no syntax errors, gets parsed as a block, even though there is block scope in JS
    foo: 2 // parsed as a label... single expression evaluation then makes the 2 work
} // another semi colon gets inserted here

所以JS正在修复你的代码......错误的方式:)

更新

我有点不清楚你到底想做什么,如果你想将一个数字分割成小数位然后分配比你首先必须确保你的数组足够大,然后你必须分割数字:

var i = 62;
var s = (i).toString(); // convert the number 62 to a string
s = i < 10 ? '0' + i : s; // make sure that "9" will become "09"
var p = s.split(''); // ["6", "2"];
arrayi[+p[0]][+p[1]] = i; // assign the value, + will convert the strings to a number without the horrible parseInt

Arrays in JavaScript are one dimensional.

So arrayi[0] is undefined, then arrayi[0][0] becomes undefined[0] which obviously doesn't work.

You need to create a two dimensional array by assigning arrays to the indexes of arrayi, there's also an error with the actual value that's being assigned:

var arrayi = []; // [] is the favored shorthand of new Array()
for (var i=0; i<=9; i++) { // always place { on the same line, 
                           // automatic semi colon insertion can screw you up in JavaScript

    arrayi[i] = []; // assign a new empty array
    for (var o=0; o<=9; o++) {
        arrayi[i][o]= i + '' + o; // i and o are both numbers so you will assign their sum
                                  // need to convert them to strings in order to concatenate them
    }
}

Concerning automatic semicolon insertion screwing you up, take a look at this:

return  // js will insert a semi colon here, so this will return undefined
{ // no syntax errors, gets parsed as a block, even though there is block scope in JS
    foo: 2 // parsed as a label... single expression evaluation then makes the 2 work
} // another semi colon gets inserted here

So JS is fixing your code... the wrong way :)

Update

It's a bit unclear to me what you exactly want to do, if you want to split a number into it's to decimal places and then assign that, than you will first have to make sure that your arrays are big enough and then you have to split the number:

var i = 62;
var s = (i).toString(); // convert the number 62 to a string
s = i < 10 ? '0' + i : s; // make sure that "9" will become "09"
var p = s.split(''); // ["6", "2"];
arrayi[+p[0]][+p[1]] = i; // assign the value, + will convert the strings to a number without the horrible parseInt
情话难免假 2024-10-16 13:51:43

我猜你的目标是这样的:

var arrayi = new Array(10);
for (var i=0; i<=9; i++)
{
    arrayi[i] = new Array(10);

    for (var o=0; o<=9; o++)
    {
        arrayi[i][o]=i + o;
    }
}

for (var i = 0; i <= 9; i++)  
    for (var j = 0; j <= 9; j++)
      document.write("arrayi[" + i + "][" + j + "]= " + arrayi[i][j] + "<br>");

I'm guessing you're aiming at something like this:

var arrayi = new Array(10);
for (var i=0; i<=9; i++)
{
    arrayi[i] = new Array(10);

    for (var o=0; o<=9; o++)
    {
        arrayi[i][o]=i + o;
    }
}

for (var i = 0; i <= 9; i++)  
    for (var j = 0; j <= 9; j++)
      document.write("arrayi[" + i + "][" + j + "]= " + arrayi[i][j] + "<br>");
一曲琵琶半遮面シ 2024-10-16 13:51:43

第六行有 i + o,其中 i 和 o 是数字,它们将作为数字添加。 0 + 0 = 06 + 2 = 8。要连接字符串,您需要将数字转换为字符串。最简单的方法是添加一个空字符串; 数组i[i][o] = i + "" + o

Your sixth line has i + o, where i and o are Numbers, which will be added as numbers. 0 + 0 = 0, and 6 + 2 = 8. To concatenate strings you need to convert the numbers to strings. The simplest way to do that is to add an empty string; arrayi[i][o] = i + "" + o

终陌 2024-10-16 13:51:43

改变这个,

arrayi[i][o]=i + o;

用;

arrayi[i][o]= i + "" + o;

Change this,

arrayi[i][o]=i + o;

with;

arrayi[i][o]= i + "" + o;
娜些时光,永不杰束 2024-10-16 13:51:43

尝试

arrayi[i][o]=i * 10 + o;

或者

arrayi[i][o]=String(i) + String(o);

Try either

arrayi[i][o]=i * 10 + o;

or

arrayi[i][o]=String(i) + String(o);
假装爱人 2024-10-16 13:51:43

要以数字方式分配它,您需要:

arrayi[i][o] = (i*10)+o;

To assign this numerically, you'd need this:

arrayi[i][o] = (i*10)+o;
等你爱我 2024-10-16 13:51:43

当您运行该代码时,您会收到错误ReferenceError:arrayi未定义

您需要先设置 arrayi[i],然后再将另一个项目分配给它,就好像它是一个数组一样。我建议尽可能使用 push 来设置数组元素。

这段代码怎么样:

var arrayi=[];for (var i=0; i<=9; i++)
  {
  arrayi.push([]);
  for (var o=0; o<=9; o++)
    { 
    arrayi[i].push([i +''+ o]);
    }
  }
  for (var i = 0, j = 9; i <= 9; i++, j--){
    console.log("arrayi[" + i + "][" + j + "]= " + arrayi[i][j]);
    }

输出:

arrayi[0][9]= 09
arrayi[1][8]= 18
arrayi[2][7]= 27
arrayi[3][6]= 36
arrayi[4][5]= 45
arrayi[5][4]= 54
arrayi[6][3]= 63
arrayi[7][2]= 72
arrayi[8][1]= 81
arrayi[9][0]= 90

这就是你想要的,对吧?

一份额外小费;我建议在 JavaScript 中使用 var array=[] 而不是 new Array()

When you run that code, you get the error ReferenceError: arrayi is not defined.

You need to set arrayi[i] before assigning another item to it as if it's an array. I suggest using push when possible to set array elements.

How about this code:

var arrayi=[];for (var i=0; i<=9; i++)
  {
  arrayi.push([]);
  for (var o=0; o<=9; o++)
    { 
    arrayi[i].push([i +''+ o]);
    }
  }
  for (var i = 0, j = 9; i <= 9; i++, j--){
    console.log("arrayi[" + i + "][" + j + "]= " + arrayi[i][j]);
    }

Output:

arrayi[0][9]= 09
arrayi[1][8]= 18
arrayi[2][7]= 27
arrayi[3][6]= 36
arrayi[4][5]= 45
arrayi[5][4]= 54
arrayi[6][3]= 63
arrayi[7][2]= 72
arrayi[8][1]= 81
arrayi[9][0]= 90

That's what you wanted, right?

One extra tip; I suggest var array=[] rather than new Array() in JavaScript.

酒绊 2024-10-16 13:51:43

document.write("arrayi[" + i + "][" + j + "]= " + a[i][j]);

你调用 a 而不是 arrayi

document.write("arrayi[" + i + "][" + j + "]= " + a[i][j]);

You call a instead of arrayi

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文