Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    May 2009
    Location
    China
    Posts
    133
    Thanks
    1
    Thanked 1 Time in 1 Post

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

  • #2
    Regular Coder
    Join Date
    May 2009
    Location
    China
    Posts
    133
    Thanks
    1
    Thanked 1 Time in 1 Post
    wait...i think i found my mistake....
    it seems that i am trying to access array elements that aren't there....right?

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by KevinJohnson View Post
    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

  • #4
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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++)
    which will solve your problem.

  • #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,505
    Thanks
    3
    Thanked 501 Times in 488 Posts
    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">
    
    <head>
      <title></title>
    </head>
    
    <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>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #6
    Regular Coder
    Join Date
    May 2009
    Location
    China
    Posts
    133
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by vwphillips View Post
    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">
    
    <head>
      <title></title>
    </head>
    
    <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.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,216
    Thanks
    75
    Thanked 4,344 Times in 4,310 Posts
    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;
    }

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,216
    Thanks
    75
    Thanked 4,344 Times in 4,310 Posts
    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
    •