0

I am trying to create a sudoku validator. For which I have written a program of return is sudoku is valid or not.

Issue: I am getting different result in console.log of same variable in different location of function

function validSolution(board) {
    //TODO
    let checklist = {
        column: false,
        row: false,
        grid: false,
    };

    function checkRepeat(arr) {
        return arr.every((el) => arr.indexOf(el) === arr.lastIndexOf(el));
    }

    function checkCount(arr) {
        return arr.sort().every((el, i) => el === i + 1 && el < 10);
    }

    function checkBoard(brd) {
        console.log(brd);
        //row
        checklist.row = brd
            .map((el) => {
                let ifRowRepeat = checkRepeat(el);
                let ifRowCount = checkCount(el);
                let resultRow =
                    ifRowRepeat === true && ifRowCount === true ? true : false;
                return resultRow;
            })
            .every((res) => res === true);

        console.log(brd);

        //Column
        let newBoard = [];
        for (let i = 0; i < 9; i++) {
            let newRow = [];
            brd.forEach((item) => {
                newRow.push(item[i]);
            });
            newBoard.push(newRow);
        }
        checklist.column = newBoard
            .map((el) => {
                let ifColRepeat = checkRepeat(el);
                let ifColCount = checkCount(el);
                let resultCol =
                    ifColRepeat === true && ifColCount === true ? true : false;
                return resultCol;
            })
            .every((res) => res === true);

        //Grid
        let gridBoard = [];
        for (let o = 0; o < 9; o = o + 3) {
            for (let n = 0; n < 9; n = n + 3) {
                let newLine = [];
                for (let l = n; l < n + 3; l++) {
                    let line = brd[l];
                    for (let m = o; m < o + 3; m++) {
                        newLine.push(line[m]);
                    }
                }
                gridBoard.push(newLine);
            }
        }
        checklist.grid = brd
            .map((el) => {
                let ifGridRepeat = checkRepeat(el);
                let ifGridCount = checkCount(el);
                let resultGrid =
                    ifGridRepeat === true && ifGridCount === true ? true : false;
                return resultGrid;
            })
            .every((res) => res === true);
    }

    checkBoard(board);
    console.log(checklist);
    return checklist.column && checklist.row && checklist.grid;
}
validSolution([
    [5, 3, 4, 6, 7, 8, 9, 1, 2],
    [6, 7, 2, 1, 9, 5, 3, 4, 8],
    [1, 9, 8, 3, 4, 2, 5, 6, 7],
    [8, 5, 9, 7, 6, 1, 4, 2, 3],
    [4, 2, 6, 8, 5, 3, 7, 9, 1],
    [7, 1, 3, 9, 2, 4, 8, 5, 6],
    [9, 6, 1, 5, 3, 7, 2, 8, 4],
    [2, 8, 7, 4, 1, 9, 6, 3, 5],
    [3, 4, 5, 2, 8, 6, 1, 7, 9],
]);

I am getting: First One is Correct:

        [ [ 5, 3, 4, 6, 7, 8, 9, 1, 2 ],
          [ 6, 7, 2, 1, 9, 5, 3, 4, 8 ],
          [ 1, 9, 8, 3, 4, 2, 5, 6, 7 ],
          [ 8, 5, 9, 7, 6, 1, 4, 2, 3 ],
          [ 4, 2, 6, 8, 5, 3, 7, 9, 1 ],
          [ 7, 1, 3, 9, 2, 4, 8, 5, 6 ],
          [ 9, 6, 1, 5, 3, 7, 2, 8, 4 ],
          [ 2, 8, 7, 4, 1, 9, 6, 3, 5 ],
          [ 3, 4, 5, 2, 8, 6, 1, 7, 9 ] ]

Second one is incorrect:

         [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
           [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
           [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
           [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
           [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
           [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
           [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
           [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
           [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ]

why?

           { column: false, row: true, grid: true }

How come if I am not manipulating the array but printing after a map function. It is giving wrong array

Tushar
  • 23
  • 2
  • 1
    `checkCount` calls `sort()` which *does* mutate the array it is called on. – VLAZ May 21 '22 at 18:18
  • But after sorting i am not storing it main array of board. I am returning true or false through that. How main array is updating? – Tushar May 22 '22 at 04:11
  • Again, `.sort()` mutates the array it's called on. You don't need to store it anywhere for that to take effect. You just have array of arrays and you sort each of the inner arrays, thus mutating them. – VLAZ May 22 '22 at 05:41

0 Answers0