// To fill a 3x3 grid to be a valid Sudoku grid, you must ensure that each row,
// column, and the 3x3 grid contains the numbers 1 through 9 without repetition.
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
// Function to fill the Sudoku grid
function fillSudokuGrid() {
let numbers = [...Array(9).keys()].map(n => n + 1);
// Shuffle the numbers randomly
shuffleArray(numbers);
// Fill the 3x3 grid row by row
let grid = Array.from({ length: 3 }, () => Array(3).fill(0));
let index = 0;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
grid[i][j] = numbers[index++];
}
}
return grid;
}
// Function to print the grid
function printGrid(grid) {
grid.forEach(row => console.log(row.join(' ')));
}
console.log("Generated 3x3 Sudoku Grid:");
printGrid(fillSudokuGrid());
/*
run:
Generated 3x3 Sudoku Grid:
6 2 9
4 1 7
3 5 8
*/