const sudokuGrid = [ [5, 3, 0, 0, 7, 0, 0, 0, 0], [6, 0, 0, 1, 9, 5, 0, 0, 0], [0, 9, 8, 0, 0, 0, 0, 6, 0], [8, 0, 0, 0, 6, 0, 0, 0, 3], [4, 0, 0, 8, 0, 3, 0, 0, 1], [7, 0, 0, 0, 2, 0, 0, 0, 6], [0, 6, 0, 0, 0, 0, 2, 8, 0], [0, 0, 0, 4, 1, 9, 0, 0, 5], [0, 0, 0, 0, 8, 0, 0, 7, 9] ]; function solveSudoku(grid) { //iterate over every cell in grid for (let row = 0; row < 9; row++) { for (let col = 0; col < 9; col++) { if (grid[row][col] === 0) { for (let num = 1; num <= 9; num++) { //check if num can be placed in current cell if (isValid(grid, row, col, num)) { grid[row][col] = num; if (solveSudoku(grid)) { return grid; } grid[row][col] = 0; } } return false; } } } //if prgram doesnt break before reaching here, all cells are filled return true; } console.log(solveSudoku(sudokuGrid));