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.
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.)