Skip to content

Commit 529d0df

Browse files
Update script.js
1 parent f261862 commit 529d0df

File tree

1 file changed

+55
-115
lines changed

1 file changed

+55
-115
lines changed

script.js

+55-115
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,83 @@
1-
// FREEZE CODE BEGIN
2-
// Puzzles to use to test your functions
3-
//puzzle
4-
let puzzle = [[ 8,9,5, 7,4,2, 1,3,6 ],
5-
[ 2,7,1, 9,6,3, 4,8,5 ],
6-
[ 4,6,3, 5,8,1, 7,9,2 ],
7-
8-
[ 9,3,4, 6,1,7, 2,5,8 ],
9-
[ 5,1,7, 2,3,8, 9,6,4 ],
10-
[ 6,8,2, 4,5,9, 3,7,1 ],
11-
12-
[ 1,5,9, 8,7,4, 6,2,3 ],
13-
[ 7,4,6, 3,2,5, 8,1,9 ],
14-
[ 3,2,8, 1,9,6, 5,4,7 ]];
15-
16-
//puzzle 2
17-
let puzzleTwo = [[ 8,9,5, 7,4,2, 1,3,6 ],
18-
[ 8,7,1, 9,6,3, 4,8,5 ],
19-
[ 4,6,3, 5,8,1, 7,9,2 ],
20-
[ 9,3,4, 6,1,7, 2,5,8 ],
21-
[ 5,1,7, 2,3,8, 9,6,4 ],
22-
[ 6,8,2, 4,5,9, 3,7,1 ],
23-
[ 1,5,9, 8,7,4, 6,2,3 ],
24-
[ 7,4,6, 3,2,5, 8,1,9 ],
25-
[ 3,2,8, 1,9,6, 5,4,7 ]];
26-
// FREEZE CODE END
1+
document.addEventListener('DOMContentLoaded', () => {
2+
const grid = document.getElementById('sudokuGrid');
3+
for (let i = 0; i < 81; i++) {
4+
const cell = document.createElement('input');
5+
cell.setAttribute('type', 'number');
6+
cell.setAttribute('min', '1');
7+
cell.setAttribute('max', '9');
8+
cell.classList.add('cell');
9+
grid.appendChild(cell);
10+
}
11+
});
12+
13+
function getPuzzle() {
14+
const cells = document.querySelectorAll('.cell');
15+
let puzzle = [];
16+
for (let i = 0; i < 9; i++) {
17+
let row = [];
18+
for (let j = 0; j < 9; j++) {
19+
const value = parseInt(cells[i * 9 + j].value) || 0;
20+
row.push(value);
21+
}
22+
puzzle.push(row);
23+
}
24+
return puzzle;
25+
}
2726

2827
function getRow(puzzle, row) {
29-
return puzzle[row]
30-
28+
return puzzle[row];
3129
}
3230

3331
function getColumn(puzzle, col) {
34-
35-
// This is an empty array to hold the values from the for loop
36-
const column = [];
37-
38-
// Loops through each row in the puzzle
39-
for (let i = 0; i < puzzle.length; i++) {
40-
// Push the value at 'col' from each row into the column array
41-
column.push(puzzle[i][col]);
42-
}
43-
// Returns the array containing the values of the specified column
44-
return column;
32+
return puzzle.map(row => row[col]);
4533
}
4634

4735
function getSection(puzzle, x, y) {
48-
49-
// Calculates the starting row and column for the 3x3 sub-grid
50-
var startRow = y * 3;
51-
var startCol = x * 3;
52-
53-
// This empty array will store the numbers from the sub-grid
36+
const startRow = y * 3;
37+
const startCol = x * 3;
5438
const section = [];
55-
56-
// Loops through the rows of the 3x3 sub-grid
57-
for (var i = startRow; i < startRow + 3; i++) {
58-
// Loops through the columns of the 3x3 sub-grid
59-
for (var j = startCol; j < startCol + 3; j++) {
60-
// Adds each number to the section array
39+
for (let i = startRow; i < startRow + 3; i++) {
40+
for (let j = startCol; j < startCol + 3; j++) {
6141
section.push(puzzle[i][j]);
6242
}
6343
}
6444
return section;
6545
}
6646

6747
function includes1To9(arr) {
68-
69-
// Checks if array is 9 elements since Sodoku sections include 9 elements
70-
if (arr.length !== 9)
71-
return false;
72-
73-
// Sorts a copy of the array in numeric order
74-
let sortedArr = arr.slice().sort((a, b) => a - b);
75-
76-
// Compares the sorted array to [1, 2, 3, 4, 5, 6, 7, 8, 9]
77-
for (let i = 0; i < 9; i++) {
78-
if (sortedArr[i] !== i + 1) {
79-
return false;
48+
if (arr.length !== 9) return false;
49+
let sortedArr = arr.slice().sort((a, b) => a - b);
50+
for (let i = 0; i < 9; i++) {
51+
if (sortedArr[i] !== i + 1) {
52+
return false;
53+
}
8054
}
81-
}
82-
83-
return true;
84-
55+
return true;
8556
}
8657

87-
console.log(includes1To9([1, 2, 3, 4, 5, 6, 7, 8, 9])); // true
88-
console.log(includes1To9([1, 1, 2, 3, 4, 5, 6, 7, 8])); // false
89-
90-
91-
92-
9358
function sudokuIsValid(puzzle) {
94-
95-
//checkRows
96-
for (let row = 0; row < 9; row++) {
97-
if (!includes1To9(getRow(puzzle, row))) {
98-
return false;
99-
}
59+
for (let row = 0; row < 9; row++) {
60+
if (!includes1To9(getRow(puzzle, row))) return false;
10061
}
101-
102-
//checkColumns
103-
for (let col = 0; col < 9; col++) {
104-
if (!includes1To9(getColumn(puzzle, col))) {
105-
return false;
106-
}
62+
for (let col = 0; col < 9; col++) {
63+
if (!includes1To9(getColumn(puzzle, col))) return false;
10764
}
108-
//checkSection
109-
for (let x = 0; x < 3; x++) {
65+
for (let x = 0; x < 3; x++) {
11066
for (let y = 0; y < 3; y++) {
111-
if (!includes1To9(getSection(puzzle, x, y))) {
112-
return false;
113-
}
67+
if (!includes1To9(getSection(puzzle, x, y))) return false;
11468
}
11569
}
116-
117-
// If all checks passed, the puzzle is valid
11870
return true;
11971
}
12072

121-
// Example usage:
122-
console.log(sudokuIsValid(puzzle)); // true
123-
console.log(sudokuIsValid(puzzleTwo)); // false (because puzzleTwo has an invalid row)
124-
125-
// YOU CAN console.log THESE RESULTS TO SEE THE OUTPUT
126-
console.log(getRow(puzzle, 8));
127-
console.log(getRow(puzzle, 0));
128-
console.log(getColumn(puzzle, 0));
129-
console.log(getColumn(puzzle, 8));
130-
console.log(getSection(puzzle, 0, 0));
131-
console.log(getSection(puzzle, 1,0));
132-
133-
// FREEZE CODE BEGIN
134-
module.exports = {
135-
getRow,
136-
getColumn,
137-
getSection,
138-
includes1To9,
139-
sudokuIsValid,
140-
puzzle,
141-
puzzleTwo
142-
};
143-
// FREEZE CODE END
73+
function validateSudoku() {
74+
const puzzle = getPuzzle();
75+
const resultElement = document.getElementById('result');
76+
if (sudokuIsValid(puzzle)) {
77+
resultElement.textContent = "The puzzle is valid!";
78+
resultElement.className = "result valid";
79+
} else {
80+
resultElement.textContent = "The puzzle is invalid.";
81+
resultElement.className = "result invalid";
82+
}
83+
}

0 commit comments

Comments
 (0)