After pasting the code below into my Chrome browser console, I expected to see a table dynamically generated and filled with content from an array. However, despite what I consider to be valid code, I keep receiving the following error message:
Uncaught TypeError: Cannot read property 'length' of undefined at :15:31
Here is the code I am using:
var array = [["A1", "B1", "C1"],
["A2", "B2", "C2"],
["A3", "B3", "C3"],
["A4", "B4", "C4"],
["A5", "B5", "C5"],
["A6", "B6", "C6"],
["A7", "B7", "C7"]],
table = document.getElementById("table");
for(var i = 0; i < array.length; i++);
{
//=== Adds a new Row
var newRow = table.insertRow(table.length);
for(var j = 0; j < array[i].length; j++);
{
//=== Adds a new cell
var cell = newRow.InsertCell(j);
//=== Adds value to the cell
cell.innerHTML = array[i][j];
}
}
Upon clicking on the 15:31 in the error message, it appears that the issue lies within the array[i].length
section of the second loop.
From my experiences with these kinds of error messages, it seems like the variable may not be globally defined. In this case, my code clearly shows that the array
variable is defined globally. Can anyone shed light on why it's still not recognized as such and provide a solution?