Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

272
Visualizações
How can I generate the board in a cleaner way?

So, I am writing a Tic Tac Toe game using native HTML, CSS, and JavaScript. I've written a class for the board that looks pretty neat, which you can see below.

class Board {
    #boardSize;
    #player1Symbol;
    #player2Symbol;

    constructor(boardSize = 3, player1Symbol = 'X', player2Symbol = 'O') {
        this.#boardSize = boardSize;
        this.#player1Symbol = player1Symbol;
        this.#player2Symbol = player2Symbol;
    }

    get boardSize() { return this.#boardSize; }
    get player1Symbol() { return this.#player1Symbol; }
    get player2Symbol() { return this.#player2Symbol; }
}

let board = new Board(3, 'X', 'O');

But I have another script that is responsible for generating the board into the HTML.

let game_ui = document.querySelector('.game-ui');

let uiHTML = '';
for (let x = 0; x < board.boardSize; x++) {
    uiHTML += `<div class="row-${x}">`;
    for (let y = 0; y < board.boardSize; y++) uiHTML += `<input type="button" class="" id="btn-${x}.${y}" value=" " />`;
    uiHTML += `</div>`;
}

game_ui.innerHTML = uiHTML;
<body>
    <div class="game-ui">
    </div>
</body>

My question is, is there a better way to write the BoardGenerator script? The class looks neat but the generator source code doesn't look all that good and is hard to read. How can I make my code more beautiful here? I know this is a bad question to ask, but still, I am curious on how I can improve the beauty of my code here.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda