
            if (grid[i][j]) {
              newGrid[i][j] = neighbors === 2 || neighbors === 3;
            } else {
              newGrid[i][j] = neighbors === 3;
            }
          }
        }

        grid = newGrid;
        drawGrid();

        if (isRunning) {
          animationId = requestAnimationFrame(updateGrid);
        }
      }

      function toggleSimulation() {
        if (!isRunning) {
          isRunning = true;
          document.getElementById("startButton").text