1
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]...

3
  • what's the error? Just one thing, i + o will not give you the right number I am afraid, you may want to try i + "" + o. Commented Dec 26, 2010 at 23:12
  • really though you should be using parseInt(i) + parseInt(o) Commented Dec 26, 2010 at 23:13
  • It should - no, must - be noted that document.write() is pretty evil and generally deprecated. Perhaps it's only here as sample code. Commented Dec 27, 2010 at 0:51

8 Answers 8

7

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
Sign up to request clarification or add additional context in comments.

2 Comments

"...even though there are no blocks in JS" There are blocks in javascript. Just not block scope. (Except for Mozilla with let.)
@patrick Yes that's what I meant, sorry for the confusion it's late here gonna fix it
2

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

1 Comment

You're right. It works without 10 parameter. You forgot about initializing arrayi[i] row of your array.
2

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

Comments

2

Change this,

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

with;

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

Comments

1

Try either

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

or

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

Comments

1

To assign this numerically, you'd need this:

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

Comments

1

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.

Comments

0

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

You call a instead of arrayi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.