开发者

Why isn't this javascript code working?

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 <开发者_JAVA百科;= 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]...


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


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


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


Change this,

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

with;

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


Try either

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

or

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


To assign this numerically, you'd need this:

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


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.


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

You call a instead of arrayi

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜