'trying to create sudoko matrix help (JS)

I'm trying to create a random board for my project but it finishes without filling the whole board(leaves 0) I converted the original code from this site: www.101computing.net/sudoku-generator-algorithm/ from python to javascript manually and checked all the functions that I created but some things were wrong. would love to get some help here thnks!

the code:


let grid = Array(9).fill().map(()=>Array(9).fill(0))
let numberList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(grid);
fillGrid(grid)
console.log(grid)
function fillGrid(grid){
    //var counter;
    // Find next empty cell
    for (i in range(0,81)){
        row = ~~(i / 9);
        col = i % 9;
        if (grid[row][col]== 0){
            shuffle(numberList);
            for (g in numberList){
                value = numberList[g]
                // Check that this value has not already be used on this row and col
                if (!checkForNumebrInRaw(grid,row,value) && !checkForNumebrInColomn(grid,col,value)){
                        // Identify which of the 9 squares we are working on
                        square = []
                        if (row<3){
                            if (col<3){
                                square = getSquare(grid,0,3,0,3);
                            }else if (col<6){
                                square = getSquare(grid,3,6,0,3);
                            }else{
                                square = getSquare(grid,6,9,0,3);
                            }
                        }
                        else if (row<6){
                            if (col<3){
                                square = getSquare(grid,0,3,3,6);
                            }else if (col<6){
                                square = getSquare(grid,3,6,3,6);
                            }else{
                                square = getSquare(grid,6,9,3,6);
                            }
                        }
                        else{
                            if (col<3){
                                square = getSquare(grid,0,3,6,9);
                            }else if (col<6){
                                square = getSquare(grid,3,6,6,9);
                            }else{
                                square = getSquare(grid,6,9,6,9);
                            }
                        }
                        // Check that this value has not already be used on this 3x3 square
                        if (!checkForNumInSquare(square,value)){
                            grid[row][col] = value;
                            if (checkGrid(grid)){
                                return true;
                            }else
                            {
                                if (fillGrid(grid)){
                                    return true;
                                }
                            }
                        }
                }
            break 
            }
            grid[row][col] = 0;
        }        
    }
    
}

function range(start, end) {
    let res=[];
    for (i=start;i<end;i++){
        res.push(i)
    }
    return res
};
function getSquare(mat,a,b,c,d) {
  var _pj_a = []
      _pj_b = range(c, d);

  for (var _pj_c = 0, _pj_d = _pj_b.length; _pj_c < _pj_d; _pj_c += 1) {
    var i = _pj_b[_pj_c];

    _pj_a.push(mat[i].slice(a, b));
  }

  return _pj_a;
};
function checkForNumebrInRaw(grid,raw,num){
    for (let i = 0; i<grid.length;i++){
        if (grid[raw][i] == num){return true;}
    }
    return false;
}
function checkForNumebrInColomn(grid,colomn,num){
    for (let i = 0; i<grid.length;i++){
        if (grid[i][colomn] == num){return true;}
    }
    return false;
}
function shuffle(array) {
    let currentIndex = array.length,  randomIndex;
  
    // While there remain elements to shuffle...
    while (currentIndex != 0) {
  
      // Pick a remaining element...
      randomIndex = Math.floor(Math.random() * currentIndex);
      currentIndex--;
  
      // And swap it with the current element.
      [array[currentIndex], array[randomIndex]] = [
        array[randomIndex], array[currentIndex]];
    }
  
    return array;
}
function checkForNumInSquare(grid,num){
    for (let i=0;i<grid.length;i++){
        for(let j=0;j<grid[i].length;j++){
            if (grid[i][j]== num){return true;}
        }
    }
    return false
}
// A function to check if the grid is full
function checkGrid(grid){
    for (let i=0;i<grid.length;i++){
        for(let j=0;j<grid[i].length;j++){
            if (grid[i][j]== 0){return false;}
        }
    }
    return true
}

output:

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


Solution 1:[1]

Ok so after a million tries i made it!

// true - row and colomn without the number
function rowAndColomnCheck(grid,row,col,num){
    function checkForNumebrInRaw(grid,row,num){
        for (let i = 0; i<grid.length;i++){
            if (grid[row][i] == num){return true;}
        }
        return false;
    }
    function checkForNumebrInColomn(grid,colomn,num){
        for (let i = 0; i<grid.length;i++){
            if (grid[i][colomn] == num){return true;}
        }
        return false;
    }
    if (!checkForNumebrInColomn(grid,col,num) && !checkForNumebrInRaw(grid,row,num)){
        return true;
    }else{
        return false;
    }
};
function range(start, end) {
    let res=[];
    for (i=start;i<end;i++){
        res.push(i)
    }
    return res
};
function getSquare(mat,a,b,c,d) {
  var _pj_a = []
      _pj_b = range(c, d);

  for (var _pj_c = 0, _pj_d = _pj_b.length; _pj_c < _pj_d; _pj_c += 1) {
    var i = _pj_b[_pj_c];

    _pj_a.push(mat[i].slice(a, b));
  }

  return _pj_a;
};
function shuffle(array) {
    let currentIndex = array.length,  randomIndex;
  
    // While there remain elements to shuffle...
    while (currentIndex != 0) {
  
      // Pick a remaining element...
      randomIndex = Math.floor(Math.random() * currentIndex);
      currentIndex--;
  
      // And swap it with the current element.
      [array[currentIndex], array[randomIndex]] = [
        array[randomIndex], array[currentIndex]];
    }
  
    return array;
};
// check if number is in sqare given
function checkForNumInSquare(grid,num){
    for (let i=0;i<grid.length;i++){
        for(let j=0;j<grid[i].length;j++){
            if (grid[i][j]== num){return true;}
        }
    }
    return false
};
// A function to check if the grid is full
function checkGrid(grid){
    for (let i=0;i<grid.length;i++){
        for(let j=0;j<grid[i].length;j++){
            if (grid[i][j]== 0){return false;}
        }
    }
    // complete
    return true
};
function fillSudoku(mat){
    for(let i in range(0,81)){
        var mainRow = ~~(i / 9);
        var mainCol = i % 9;
        if (mat[mainRow][mainCol] == 0){
            shuffle(numberList);
            for (let value of numberList){
                if(rowAndColomnCheck(mat,mainRow,mainCol,value)){
                    var mySquare = [[]];
                    if(mainRow<3){
                        if (mainCol<3){
                            mySquare = getSquare(mat,0,3,0,3);
                        }
                        else if (mainCol<6){
                            mySquare = getSquare(mat,3,6,0,3);
                        }
                        else{
                            mySquare = getSquare(mat,6,9,0,3);
                        }
                    }
                    else if(mainRow<6){
                        if (mainCol<3){
                            mySquare = getSquare(mat,0,3,3,6);
                        }
                        else if (mainCol<6){
                            mySquare = getSquare(mat,3,6,3,6);
                        }
                        else{
                            mySquare = getSquare(mat,6,9,3,6);
                        }
                    }
                    else{
                        if (mainCol<3){
                            mySquare = getSquare(mat,0,3,6,9);
                        }
                        else if (mainCol<6){
                            mySquare = getSquare(mat,3,6,6,9);
                        }
                        else{
                            mySquare = getSquare(mat,6,9,6,9);
                        }
                    }
                    bol_t = checkForNumInSquare(mySquare,value);
                    if (bol_t == false){
                        mat[mainRow][mainCol] = value;
                        if (checkGrid(mat)){
                            return true;
                        }
                        else{
                            if (fillSudoku(mat)){
                                return true;
                            }
                        }
                    }
                }
            }
            break 
        }
    }
    mat[mainRow][mainCol] = 0;
};

var numberList = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var grid = Array(9).fill().map(()=>Array(9).fill(0));
fillSudoku(grid);
console.log(grid);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bar maizel