Ravi
Ravi

Reputation: 971

Javascript error: Uncaught TypeError: Cannot read property 'length' of undefined

I am trying to learn Javascript and have wrote a function that simply tries to solve the sudoku puzzle (on the body load of the HTML page).

However, I am receiving the following error

Uncaught TypeError: Cannot read property 'length' of undefined
    at solve (sudoku.html:33)
    at test (sudoku.html:15)
    at onload (sudoku.html:3)

I tried printing the board[row] and it is giving undefined (however I was able to print the board)

<!DOCTYPE HTML>
<html>
<body onload="test()">
    <script type="text/javascript">
        function test() {
            sudokuBoard = [ [ 8, 0, 3, 9, 0, 5, 0, 0, 0 ],
                    [ 0, 0, 0, 0, 0, 1, 0, 9, 0 ],
                    [ 0, 9, 0, 0, 4, 0, 6, 0, 0 ],
                    [ 0, 6, 9, 0, 0, 0, 0, 4, 0 ],
                    [ 0, 0, 0, 1, 0, 4, 0, 0, 0 ],
                    [ 0, 3, 0, 0, 0, 0, 5, 2, 0 ],
                    [ 0, 0, 6, 0, 2, 0, 0, 1, 0 ],
                    [ 0, 5, 0, 7, 0, 0, 0, 0, 0 ],
                    [ 0, 0, 0, 5, 0, 3, 2, 0, 6 ] ];
            solve(sudokuBoard);
        }

        function solveSudoku(sudokuArray) {
            solve(sudokuArray, 0, 0);
        }

        function solve(board, row, col) {
            if (row == board.length) {
                print(board);
                return;
            }

            nextRow = 0;
            nextCol = 0;

            if (col == board[row].length - 1) { // Move to next row
                nextRow = row + 1;
                nextCol = 0;
            } else { // Move to next column on the same row
                nextRow = row;
                nextCol = col + 1;
            }

            if (board[row][col] != 0) {
                solve(board, nextRow, nextCol);
            } else {
                for (posNum = 1; posNum <= 9; posNum++) {
                    if (isValid(board, row, col, posNum)) {
                        board[row][col] = posNum;
                        solve(board, nextRow, nextCol);
                        board[row][col] = 0;
                    }
                }
            }
        }

        function isValid(board, row, col, posNum) {
            // Check on the row
            for (i = 0; i < board[row].length; i++) {
                if (board[row][i] == posNum) {
                    return false;
                }
            }
            // Check the column
            for (i = 0; i < board[col].length; i++) {
                if (board[i][col] == posNum) {
                    return false;
                }
            }

            // Check the 3x3 sub matrix
            rowStartIndex = (row / 3) * 3;
            columnStartIndex = (col / 3) * 3;
            for (i = rowStartIndex; i < 3; i++) {
                for (j = columnStartIndex; j < 3; j++) {
                    if (board[rowStartIndex + i][columnStartIndex + j] == posNum) {
                        return false;
                    }
                }
            }
            return true;
        }

        function print(sudokuArray) {
            console.log(sudokuArray);
        }
    </script>
</body>
</html>

Any help on this?

Upvotes: 0

Views: 89

Answers (1)

Prerna Jain
Prerna Jain

Reputation: 1240

Your test function should ideally be :

function test() {
            sudokuBoard = [ [ 8, 0, 3, 9, 0, 5, 0, 0, 0 ],
                    [ 0, 0, 0, 0, 0, 1, 0, 9, 0 ],
                    [ 0, 9, 0, 0, 4, 0, 6, 0, 0 ],
                    [ 0, 6, 9, 0, 0, 0, 0, 4, 0 ],
                    [ 0, 0, 0, 1, 0, 4, 0, 0, 0 ],
                    [ 0, 3, 0, 0, 0, 0, 5, 2, 0 ],
                    [ 0, 0, 6, 0, 2, 0, 0, 1, 0 ],
                    [ 0, 5, 0, 7, 0, 0, 0, 0, 0 ],
                    [ 0, 0, 0, 5, 0, 3, 2, 0, 6 ] ];
            solveSudoku(sudokuBoard);
        }

Correction :

function called inside test() should be solveSudoku() instead of solve()

You are getting this 'undefined' error as inside the solve(), value of row and col is undefined.

So board[undefined].length would throw error.

Upvotes: 1

Related Questions