Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Compliant way of Declaring Arrays

1. ## Compliant way of Declaring Arrays

I have a problem with my code that seems to be...confusing to say the least.
i'm trying to sent the contents of an array to a variable (in a for..next loop). The problem is, when i try to access that Array, the Error Console says: "Coord[x] is undefined" however, i know that it is defined.
Am i declaring the variable in a depreciated way?

What the array should be storing is an x,y position, and at that x,y position is the name of the square on the chess board (e.g. A1, E8, etc..)

here's the code:

Code:
```var blockSize = 50;

// Assign Array values to  Chess Board Coordinates

var Coord = new Array(9);
for (i=1; i <9; i++)
{
Coord[i]= new Array(9);
}

var Letter = ["A", "B", "C", "D", "E", "F", "G", "H"];
var BoardIndex = 1;
var LetterIndex = 1;

for (var x = 1; x < blockSize * 8; x = x + blockSize)
{
for (var y = 1; y < blockSize * 8; y = y + blockSize)
{
Coord[x][y] = Letter[LetterIndex] + BoardIndex;
BoardIndex++;
LetterIndex++;

if (BoardIndex > 8)
{
BoardIndex = 1;      // Start counting the numbers from 1
}

if (LetterIndex > 8)     // Start counting the letters from A
{
LetterIndex = 1;
}

}
}```

• wait...i think i found my mistake....
it seems that i am trying to access array elements that aren't there....right?

• Originally Posted by KevinJohnson
I have a problem with my code that seems to be...confusing to say the least.
i'm trying to sent the contents of an array to a variable (in a for..next loop). The problem is, when i try to access that Array, the Error Console says: "Coord[x] is undefined" however, i know that it is defined.
it's right, is not defined! as a rule I never argue with error console,

you use index from 1 to 9, not the letter 'x'.

best regards

• No, that's not the problem. You are not trying to access Count['x']. Your problem is the following:

You are looping over the coordinates, and not over the array indices, so while trying to set Count[x][y], x is a coordinate (a multiple of blocksize) and thus Count[x] is not defined.

Since you don't actually use x or y anywhere else except for this, you can safely change
Code:
```for (var x = 1; x < blockSize * 8; x = x + blockSize)
{
for (var y = 1; y < blockSize * 8; y = y + blockSize)```
to
Code:
```for (var x = 1; x <= 8; x ++)
{
for (var y = 1; y <= 8; y++)```

• Code:
```<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<title></title>

<body>
<script> vic=0; </script>
<form name=Show id=Show style="position:absolute;visibility:visible;top:0px;left:0px;" >
<input size=100 name=Show0 >
<input size=10 name=Show1 >
<input size=10 name=Show2 >
<input size=10 name=Show3 >
<input size=10 name=Show4 >
<input size=10 name=Show5 >
<input size=10 name=Show6 >
<input size=10 name=Show7 >
<input size=10 name=Show8 >
<input size=10 name=Show9 ><br>
<textarea name=TA rows=10 cols=100 ></textarea>
</form>

<script type="text/javascript">
/*<![CDATA[*/

var blockSize = 50;

// Assign Array values to  Chess Board Coordinates

var Coord = [];
for (i=0; i <8; i++)
{
Coord[i]= [];
}

var Letter = ["A", "B", "C", "D", "E", "F", "G", "H"];
var BoardIndex = 1;
var LetterIndex = 0;

for (var letter,x = 0; x <8; x++)
{
letter=Letter[LetterIndex];
for (var y = 0; y < 8; y++)
{
Coord[x][y] = [letter+(y+1),x*blockSize,y*blockSize];
//document.Show.TA.value+=Coord[x][y]+'\n';

}
LetterIndex++;
}
document.Show.TA.value+=Coord.join('\n');

/*]]>*/
</script>

</body>

</html>```

• Originally Posted by vwphillips
Code:
```<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<title></title>

<body>
<script> vic=0; </script>
<form name=Show id=Show style="position:absolute;visibility:visible;top:0px;left:0px;" >
<input size=100 name=Show0 >
<input size=10 name=Show1 >
<input size=10 name=Show2 >
<input size=10 name=Show3 >
<input size=10 name=Show4 >
<input size=10 name=Show5 >
<input size=10 name=Show6 >
<input size=10 name=Show7 >
<input size=10 name=Show8 >
<input size=10 name=Show9 ><br>
<textarea name=TA rows=10 cols=100 ></textarea>
</form>

<script type="text/javascript">
/*<![CDATA[*/

var blockSize = 50;

// Assign Array values to  Chess Board Coordinates

var Coord = [];
for (i=0; i <8; i++)
{
Coord[i]= [];
}

var Letter = ["A", "B", "C", "D", "E", "F", "G", "H"];
var BoardIndex = 1;
var LetterIndex = 0;

for (var letter,x = 0; x <8; x++)
{
letter=Letter[LetterIndex];
for (var y = 0; y < 8; y++)
{
Coord[x][y] = [letter+(y+1),x*blockSize,y*blockSize];
//document.Show.TA.value+=Coord[x][y]+'\n';

}
LetterIndex++;
}
document.Show.TA.value+=Coord.join('\n');

/*]]>*/
</script>

</body>

</html>```
what exactly does the code actually do?:

Code:
`document.Show.TA.value+=Coord.join('\n');`
why the need for writing all of those input size things:

it seems very obsecure.

• I'm just curious as to what the POINT of all this is.

It would be so much simpler to just create a Coord( ) function that takes two arguments, thus:
Code:
```function Coord( x, y )
{
return ("?ABCDEFGH").characterAt(x) + y;
}```
Presto.

Coord( 3, 5 ) ==>> "C5"
Coord( 5, 3 ) ==>> "E3"
and so on.

If you wanted to be paranoid:
Code:
```function Coord( x, y )
{
x = parseInt(x); y = parseInt(y);
if ( isNaN(x) || x < 1 || x > 8 || isNaN(y) || y < 1 || y > 8 ) return "*error*";
return ("?ABCDEFGH").characterAt(x) + y;
}```

• But if you think you must have an array:
Code:
```var Coord = new Array( );
for ( var x = 1; x <= 8; ++x )
{
var row = new Array( );
var letter = ("?ABCDEFGH").characterAt(x);
for ( var y = 1; y <= 8; ++ y )
{
row[y] = letter + y;
}
Coord[x] = row;
}```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•