Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

269
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda