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

152
Visualizações
How do I implement a 'tracking' object inside of my class constructor?

I've built a Binary Sudoku web-app that utilizes a chain of recursive functions to generate a random solved grid, at which point the grid is cut down, by some reduction functions, to a few starting tiles for players to reassemble (solve). For the sake of my own curiosity, I am trying to build a constructor object inside of my main generator class that will keep track of how many times each recursive function is ran, each time I generate a new board (not interested in persisting this data past a single board instance). I would also like to log the total runtime of this operation after the board has been generated.

So far, I have attempted to implement this object inside of my constructor:

this.stats = {
        rowGenerator: 0,
        rowAssembler: 0,
        colTotalCheck: 0,
        colUniqueCheck: 0,
        runtime: 0
    };

Inside each of my four recursive functions, I've placed these increments at the top:

this.stats.rowGenerator++;
this.stats.rowAssembler++;
this.stats.colTotalCheck++;
this.stats.colUniqueCheck++;

Currently, this is returning a TypeError for each reference to respective 'stats' values.

Uncaught TypeError: Cannot read properties of undefined (reading 'colUniqueCheck')

I've also placed timing methods before and after the recursive chain to log runtime, but unfortunately the only timing feedback I'm receiving back is '0ms'. The timing methods are written as such:

startTime() {
    // before recursive chain
    return performance.now();
};
 endTime() {
    // after recursive chain
    return performance.now();
};
totalTime() {
    const end = this.endTime();
    const start = this.startTime();
    return `Runtime: ${end - start}ms`;
};

I've searched for similar problems but cannot find any useful information that is unique to these problems, perhaps tracking these trivial little stats is unrealistic inside of a class.

Any insight is appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are calling this.checkColumnsUnique (which accesses this.stats) before you even initialize this.stats:

    this.fillBoard = this.checkColumnsUnique();
    this.stats = {
        rowGenerator: 0,
        rowAssembler: 0,
        colTotalCheck: 0,
        colUniqueCheck: 0,
        runtime: 0
    };

If you reverse the order of these two statements, it will work.

(There is one other issue though - in checkColumnsUnique you call checkColumnsUnique without this. in front.)

about 4 years ago · Juan Pablo Isaza Relatório
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