I am trying to understand why my array updates its values before I want to apply changes to it with the function createRandomGrid
. When I print the array before and after the modification, it prints the same result.
Can someone please explain me how this occurs?
let sudokuGrid = [
[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]
];
console.log(sudokuGrid)
createRandomGrid(sudokuGrid);
console.log(sudokuGrid)
function createRandomGrid(board) {
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
if (board[i][j] === 0) {
for (let k = 1; k <= 9; k++) {
let num = Math.floor(Math.random() * 9) + 1;
if (isValidNode(i, j, num, board)) {
board[i][j] = num;
if (createRandomGrid(board)) return true;
board[i][j] = 0;
}
}
return false;
}
}
}
return true;
}