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

143
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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