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

142
Views
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 answers
Answer question

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 Report
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!