I write the game "Life". How to find adjacent cells?
window.onload = function() { var cell = [ 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ]; var tableContainer = document.querySelector("table"); var tbodyContainer = tableContainer.querySelector("tbody"); var createdRow = document.createElement("tr"); cell.forEach(function(Onecell, i, arr) { if (i % 4 === 0) { tbodyContainer.appendChild(createdRow); createdRow = document.createElement("tr"); } tbodyContainer.appendChild(createdRow); var createdCell = document.createElement("td"); if (Onecell === 0) { createdCell.classList.add("dead"); } else { createdCell.classList.add("live"); } // добавляем только что созданый элемент в дерево DOM createdRow.appendChild(createdCell); }); }
td { margin: 10px; border: 2px solid black; } tr { display: table-row; vertical-align: inherit; border-color: inherit; } .live, .dead { width: 30px; height: 30px; } .live { background-color: #adff2f; } .dead { background-color: #ffffff; } table { border: 4px double black; }
<table> <tbody> </tbody> </table>