I'm currently working on a project that involves creating a 2D array. I want the interior elements of this array to be black while the exterior elements should be white. However, my 2D array doesn't seem to be forming correctly - it looks more like two separate arrays put together. Can you point out what mistake I might be making here? Also, is there a simpler way to achieve this task?
Check out this image for a clearer understanding of my problem and goal.
Here's the code I've worked on so far:
function createTable() {
var table = document.createElement('table');
var rows = +document.getElementById('Rows').value;
var cols = +document.getElementById('Cols').value;
for(var r=0; r<rows; r++) {
var tr = document.createElement('tr');
table.appendChild(tr);
for(var c=0; c<cols; c++) {
if(r == 0 || c == 0 || r == rows - 1 ||c == cols - 1 ){
var td = document.createElement('td');
tr.appendChild(td);
var inp = document.createElement('input');
inp.setAttribute('type','text');
td.appendChild(inp);
} else {
var tq = document.createElement('tq');
tr.appendChild(tq);
var inp = document.createElement('input');
inp.setAttribute('type','text');
tq.appendChild(inp);
}
}
}
var container = document.getElementById('input_container');
container.innerHTML = '';
container.appendChild(table);
}
td>input {
width: 30px;
height: 30px;
padding: 5px;
font-size: 20px;
}
tq>input {
width: 30px;
height: 30px;
padding: 5px;
background-color: #000000;
font-size: 20px;
}
Rows : <input type="text" id="Rows" value="3">
Cols : <input type="text" id="Cols" value="8">
<button onclick="createTable();">Create</button>
<div id="input_container"></div>