1

I am brand new to JavaScript and still figuring out the way things interact, so I apologize if this is obvious. I am ultimately trying to create a sliding tile puzzle, but at the moment I need to create a 2 dimensional array that will populate my grid with values (1-8). Basically all of my information thus far has been gathered from internet searches as my other resources have proven to be pretty useless.

Here is my code that generates a grid:

function newPuzzle(r, c)
{
   var table = document.createElement("table");

   for (var i = 0; i < r; i++)
   {
      var row = document.createElement('tr');
      for (var j = 0; j < c; j++)
      {
         var column = document.createElement('td');

         if (i%2 == j%2)
         {
               column.className = "even";
         }
         else
         {
               column.className = "odd";
         }     
         row.appendChild(column);    
      }
      table.appendChild(row);
   }
   document.body.appendChild(table);

   populateTable();
}

At the end I've called the populateTable() function (which I'm not even sure will work) and here is the code for that:

function populateTable()
{

   var cell = new Array(_r);
   for (var i = 0; i < _r; i++)
   {
      cell[i] = new Array(_c);
   }

   for (var i = 0; i < cell.length; ++i)
   {
        var entry = cell[i];
        for (var j = 0; j < entry.length; ++j)
        {
            var gridTable = document.getElementByTagName("table");
            var _cells = gridTable.rows[i].cells[j].innerHTML = "2"; 
            //the 2 is just for testing
        }

   }
}

Any insight would be very appreciated.

1 Answer 1

1

Your code basically had two issues, when I ran it:

  1. In your method populateTable() the variables _r and _c were not defined, so I passed them in as arguments.

  2. The method document.getElementByTagName does not exists, it's plural document.getElementsByTagName which in turn returns an array of all <table> elements so you have to select which one you want - I opted for the first one since I assume you only have one table on your page.

Here are the changes:

function newPuzzle(r, c)
{
   var table = document.createElement("table");

   for (var i = 0; i < r; i++)
   {
      var row = document.createElement('tr');
      for (var j = 0; j < c; j++)
      {
         var column = document.createElement('td');

         if (i%2 == j%2)
         {
               column.className = "even";
         }
         else
         {
               column.className = "odd";
         }     
         row.appendChild(column);    
      }
      table.appendChild(row);
   }
   document.body.appendChild(table);
   // here we pass _r and _c as arguments
   populateTable(r,c);
}

function populateTable(_r,_c)
{

   var cell = new Array(_r);
   for (var i = 0; i < _r; i++)
   {
      cell[i] = new Array(_c);
   }

   for (var i = 0; i < cell.length; ++i)
   {
        var entry = cell[i];
        for (var j = 0; j < entry.length; ++j)
        {
            // getElementsByTagName returns an array of all table elements
            var gridTable = document.getElementsByTagName("table");

            // we select the first table element with the array index [0]
            var _cells = gridTable[0].rows[i].cells[j].innerHTML = "2"; 
            //the 2 is just for testing
        }
   }
}

An interactive JS fiddle: https://jsfiddle.net/Ls76kk2a/2/

An optimization tip: Give your table a unique ID like this:

var table = document.createElement("table");
table.id = "mySuperCoolTable";

Then you can be sure you get the right one with:

var gridTable = document.getElementById("mySuperCoolTable");

This only returns one (or none) table because the ID must be unique.

Response to your comment:

Here's an example how to populate all elements but the last one:

function populateTable(_r,_c)
{

   var cell = new Array(_r);
   for (var i = 0; i < _r; i++)
   {
      cell[i] = new Array(_c);
      for (var j = 0; j < _c; j++) {
        cell[i][j] = i*_c + j;
      }
   }

   // fix the last one
   cell[_r-1][_c-1] = "X";

   for (var i = 0; i < cell.length; ++i)
   {
        var entry = cell[i];
        for (var j = 0; j < entry.length; ++j)
        {
            var gridTable = document.getElementsByTagName("table");
            var _cells = gridTable[0].rows[i].cells[j].innerHTML = cell[i][j]; 
        }
   }
}

And here's the JS fiddle: https://jsfiddle.net/Ls76kk2a/3/

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! Any idea on how to go about populating all but 1 cell? With my last line in the populate() function, I'm not sure what to set the innerHTML to.
You are a lifesaver. Thaaaaank you!

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.