...

Compliant way of Declaring Arrays

KevinJohnson
05-18-2009, 01:36 PM
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:

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;
}

}
}

KevinJohnson
05-18-2009, 01:57 PM
wait...i think i found my mistake....
it seems that i am trying to access array elements that aren't there....right?

oesxyl
05-18-2009, 02:02 PM
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

venegal
05-18-2009, 02:06 PM
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
for (var x = 1; x < blockSize * 8; x = x + blockSize)
{
for (var y = 1; y < blockSize * 8; y = y + blockSize)
to
for (var x = 1; x <= 8; x ++)
{
for (var y = 1; y <= 8; y++)

vwphillips
05-18-2009, 02:14 PM
<!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>

KevinJohnson
05-18-2009, 08:38 PM
<!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?:

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

why the need for writing all of those input size things:

it seems very obsecure.

Old Pedant
05-19-2009, 02:44 AM
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:

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:

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;
}

Old Pedant
05-19-2009, 02:50 AM
But if you think you must have an array:

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;
}