Suduko Solver Calling function but undefined

I wrote this function in codepen then where it was working perfectly when I transferred it over to replit if I call the checkColPlacement function in the solve function I get the following error:

TypeError: Cannot read properties of undefined (reading ‘checkColPlacement’)
at pencilArr (/home/runner/boilerplate-project-sudoku-solver/controllers/sudoku-solver.js:163:20)

If I call the function in a console.log with the same initial arguments (puzzleString, a, 1, 1) in the check region function it gives the correct output so it is only undefined when called in the solve function doesn’t make any sense to me.

here is the code of the solve function that is throwing the error:


 solve(puzzleString) {

    
let coordinate = ['a1','a2','a3','a4','a5','a6','a7','a8','a9','b1','b2','b3','b4','b5','b6','b7','b8','b9','c1','c2','c3','c4','c5','c6','c7','c8','c9','d1','d2','d3','d4','d5','d6','d7','d8','d9','e1','e2','e3','e4','e5','e6','e7','e8','e9','f1','f2','f3','f4','f5','f6','f7','f8','f9','g1','g2','g3','g4','g5','g6','g7','g8','g9','h1','h2','h3','h4','h5','h6','h7','h8','h9','i1','i2','i3','i4','i5','i6','i7','i8','i9'] 

/*pencilfunction*/
function pencilArr(puzzleString) {

let pencil = []
for (let j = 0; j <= 80; j++) {
pencil.push([])
if (puzzleString[j] != ".") {
  pencil[j].push("solved")
  pencil[j].push(puzzleString[j])
}
 else {
for (let i = 1; i <= 9; i++) {
  console.log(puzzleString)
  console.log(coordinate[j][0])
  console.log(coordinate[j][1])
  console.log(i)
  
  
  console.log(this.checkColPlacement(puzzleString,"a",1, 1))
  
let pencilColumn = this.checkColPlacement(puzzleString, coordinate[j][0], coordinate[j][1], i)
 
let pencilRow = this.checkRowPlacement(puzzleString, coordinate[j][0], coordinate[j][1], i)
let pencilRegion = this.checkRegionPlacement(puzzleString, coordinate[j][0], coordinate[j][1], i)
if (pencilRegion != 'truebox' && pencilColumn != 'truecol' && pencilRow != 'truerow') {
  pencil[j].push(i)
}
}
}

link to the repit:

boilerplate-project-sudoku-solver - Replit

I got it to work by copying the checkcolplacement, checkrowplacement, and checkregionplacement fumctions into the solve function. Doing it this way kind of defeats the point of writing fuctions though. Im still dont understand why when the functions were outside of the solve function they were undefined.

Isn’t it just a this issue?

We can’t see all the code but do you have a function definition inside a method? Why would a method be on this inside that function?

class User {
  constructor(name) {
    this.name = name;
  }

  sayHi() {
    return this.name;
  }

  testThis() {
    console.log(this.name); // John
    console.log(this.sayHi()); // John
    function nestedFn() {
      console.log(this.name); // undefined
      console.log(this.sayHi()); // undefined
    }
    nestedFn();
  }
}

let user = new User("John");
user.testThis();

You really need to format your code. Replit can do it for you, click the three dots to the far right of the file tab, click the file name, then click format.

Ill look into it tommorow ive tried calling the function with and without this and it doesnt work. Im still a little confused with the use of this and should probably look into it more. Thanks for the reply seems like you allways have some constructive answers.

In the following code I am able to call the function but if I try to call the function in my finished code it gives me an undefined error. Its very confusing. My code works fine but its super repetitive because I have to copy all the functions into the solve function.


class SudokuSolver {

  validate(puzzleString) {
  }

  checkRowPlacement(puzzleString, row, column, value) {
return 'row'
  }

  checkColPlacement(puzzleString, row, column, value) {
return 'column'
  }

  checkRegionPlacement(puzzleString, row, column, value) {
   
return 'region'
  }

  
  
  solve(puzzleString) {
    let functcall = this.checkColPlacement('aaas', "a", 1, 5)
    return functcall + "hi"
  }
 
}

let solver = new SudokuSolver()





module.exports = SudokuSolver;