# sudoku problem

unknown
javascript
a year ago
2.3 kB
4
Indexable
Never
```//puzzle
let puzzle = [[ 8,9,5,   7,4,2,   1,3,6 ],
[ 2,7,1,   9,6,3,   4,8,5 ],
[ 4,6,3,   5,8,1,   7,9,2 ],

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

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

//puzzle 2
let puzzleTwo = [[ 8,9,5,7,4,2,1,3,6 ],
[ 8,7,1,9,6,3,4,8,5 ],
[ 4,6,3,5,8,1,7,9,2 ],
[ 9,3,4,6,1,7,2,5,8 ],
[ 5,1,7,2,3,8,9,6,4 ],
[ 6,8,2,4,5,9,3,7,1 ],
[ 1,5,9,8,7,4,6,2,3 ],
[ 7,4,6,3,2,5,8,1,9 ],
[ 3,2,8,1,9,6,5,4,7 ]];

//DO NOT EDIT ABOVE

//get current row
function getRow(puzzle, row) {
return puzzle[row];
}

//get current column
function getColumn(puzzle, col) {

return puzzle.map( x=> x[col]);
}
//get current subgrid
function getSection(puzzle, x, y) {

let arr = []
let xStart = x * 3;
let yStart = y * 3;

for (let i = 0; i< 3; i ++){
for(let j = 0; j <3; j++){
arr.push(puzzle[i + yStart][j + xStart]);
}
}
return arr;
}

//find out if arr includes 1 to 9

function includes1To9(arr) {
let arr2 = [1,2,3,4,5,6,7,8,9];
let sortedArr = arr.sort((a, b) => a - b);

if(arr2.join(',') === sortedArr.join(',')){
return true;
}
else{
return false;
}
}

function checkIfArrayIsUnique(myArray) {
return myArray.length === new Set(myArray).size;
}

//validate is sudoku inputs are valid
function sudokuIsValid(puzzle) {
//check rows;
for (let i = 0; i < puzzle.length; i++){
let row = getRow(puzzle, i);
if(includes1To9(row) === false){
return false;
}
if(checkIfArrayIsUnique(row) === false){
return false;
}
}
//check columns;
for(let j = 0; j< 9; j++){
let column = getColumn(puzzle, j);
if(includes1To9(column) === false){
return false;
}
if(checkIfArrayIsUnique(column) === false){
return false;
}
}
return true;
}

//returns error expected sudokuIsValid(puzzle) to be true, received: false

```