Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!