imbes
imbes

Reputation: 63

uncaught typeerror in class despite setting element

So I'm making a basic brute force sudoku solver. I created a class that takes in a board string and use a constructor to set this.board = board parameter passed in

export default class SudokuSolver {
  constructor(board){
    this.board = board
  }

the first method that gets run is validBoardFormat

validBoardFormat(){
    /* possible input formats: CSV of 81 integers or string of 81 integers
    0's replace unknown numbers*/
    let cleanedBoard=[];
    let uncleanedBoard = this.board.split('') //split by character
    for (let i = 0; i<uncleanedBoard.length; i++){
      if (this.validNum(uncleanedBoard[i])){ //validate with validNum function
        cleanedBoard.push(parseInt(uncleanedBoard[i]))
      }
    }
    if (cleanedBoard.length === 81){ 
      this.board = cleanedBoard //make sure board matches correct sudoku board length
      return true
    }
    return false
  }

Let's say for example pass in:

let boardString = "1024091501202523235" //imagine this is a valid board string
const board = new SudokuSolver(boardString)
console.log(board.validBoardFormat())

console: true

This works perfectly fine on its own.

later in the class I define a solve method

solve(){
    if (this.validBoardFormat()){
        this.validBoardFormat()
        this.configureBoardArray()
        this.solveBoard()

    } else {
      console.log('Board error')
    }
  }

The issue is when I pass this:

let boardString = "1024091501202523235" //imagine this is a valid board string
const board = new SudokuSolver(board)
console.log(board.solve())

and I run it on liveserver, I am getting this error:

Uncaught TypeError: this.board.split is not a function in the validBoardFormat function

I have been trying to troubleshoot this for a while now any help would be appreicated

Upvotes: 0

Views: 25

Answers (3)

imbes
imbes

Reputation: 63

I realize now that in my solve method, I was doing validBoardFormat within the if statement itself, which alters the value of this.board from a string(original input) to an array. After it passes the if statement, it runs this.validBoardFormat again but now this.board is an array and so it cant do .split() anymore.

to fix this i have stored the return of this.validBoardFormat in a variable then use that value to continue on

    solve(){
      let validation = this.validBoardFormat()
      if (validation){
        this.configureBoardArray()
        this.solveBoard()
        return this.board
      } else {
        console.log('Board error')
      }
    }

Weird how quickly you can find your own bugs when you finally ask for help after staring at it for a long time beforehand lmao

Upvotes: 0

ray
ray

Reputation: 27245

You're setting this.board to an array:

if (cleanedBoard.length === 81){
  this.board = cleanedBoard // <= this.board is now an array
  return true
}

So the next time you call validBoardFormat() you end up trying to invoke split on the array, not the string:

let uncleanedBoard = this.board.split('') // this.board is now an array, not a string

Upvotes: 1

John Targaryen
John Targaryen

Reputation: 1194

You can't do const board = new SudokuSolver(board) because the board variable isn't defined yet. You need to do const board = new SudokuSolver(boardString).

Upvotes: 1

Related Questions