Skip to content

Commit 52bc78c

Browse files
Update script.js
1 parent b84863d commit 52bc78c

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

script.js

+44-24
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
document.addEventListener('DOMContentLoaded', () => {
22
const grid = document.getElementById('sudokuGrid');
33

4-
// Generate 9 sections (3x3 blocks)
5-
for (let section = 0; section < 9; section++) {
6-
const sectionDiv = document.createElement('div');
7-
sectionDiv.classList.add('section');
8-
9-
// Create 9 cells per section
10-
for (let i = 0; i < 9; i++) {
4+
// Create 9x9 grid
5+
for (let row = 0; row < 9; row++) {
6+
for (let col = 0; col < 9; col++) {
117
const cell = document.createElement('input');
128
cell.setAttribute('type', 'number');
139
cell.setAttribute('min', '1');
1410
cell.setAttribute('max', '9');
15-
cell.classList.add('cell');
16-
sectionDiv.appendChild(cell);
11+
cell.classList.add('cell', 'bg-white', 'border', 'border-gray-300', 'text-center');
12+
cell.style.width = '3rem';
13+
cell.style.height = '3rem';
14+
15+
// Add thicker borders for 3x3 sections
16+
if (row % 3 === 0 && row !== 0) cell.style.borderTopWidth = '2px';
17+
if (col % 3 === 0 && col !== 0) cell.style.borderLeftWidth = '2px';
18+
19+
grid.appendChild(cell);
1720
}
18-
grid.appendChild(sectionDiv);
1921
}
2022
});
2123

24+
// Update specific row in the grid
25+
function updateRow() {
26+
const rowIndex = parseInt(document.getElementById('rowSelect').value);
27+
const rowInput = document.getElementById('rowInput').value;
28+
29+
// Validate input
30+
if (rowInput.length !== 9 || !/^\d+$/.test(rowInput)) {
31+
alert('Please enter exactly 9 digits (1-9).');
32+
return;
33+
}
34+
35+
const cells = document.querySelectorAll('.cell');
36+
for (let i = 0; i < 9; i++) {
37+
const cellIndex = rowIndex * 9 + i;
38+
cells[cellIndex].value = rowInput[i];
39+
}
40+
}
41+
2242
function getPuzzle() {
2343
const cells = document.querySelectorAll('.cell');
2444
let puzzle = [];
@@ -28,7 +48,6 @@ function getPuzzle() {
2848
const value = parseInt(cell.value) || 0;
2949
row.push(value);
3050

31-
// Add row to puzzle every 9 cells
3251
if ((index + 1) % 9 === 0) {
3352
puzzle.push(row);
3453
row = [];
@@ -38,6 +57,17 @@ function getPuzzle() {
3857
return puzzle;
3958
}
4059

60+
function includes1To9(arr) {
61+
if (arr.length !== 9) return false;
62+
let sortedArr = arr.slice().sort((a, b) => a - b);
63+
for (let i = 0; i < 9; i++) {
64+
if (sortedArr[i] !== i + 1) {
65+
return false;
66+
}
67+
}
68+
return true;
69+
}
70+
4171
function getRow(puzzle, row) {
4272
return puzzle[row];
4373
}
@@ -58,17 +88,6 @@ function getSection(puzzle, x, y) {
5888
return section;
5989
}
6090

61-
function includes1To9(arr) {
62-
if (arr.length !== 9) return false;
63-
let sortedArr = arr.slice().sort((a, b) => a - b);
64-
for (let i = 0; i < 9; i++) {
65-
if (sortedArr[i] !== i + 1) {
66-
return false;
67-
}
68-
}
69-
return true;
70-
}
71-
7291
function sudokuIsValid(puzzle) {
7392
for (let row = 0; row < 9; row++) {
7493
if (!includes1To9(getRow(puzzle, row))) return false;
@@ -87,11 +106,12 @@ function sudokuIsValid(puzzle) {
87106
function validateSudoku() {
88107
const puzzle = getPuzzle();
89108
const resultElement = document.getElementById('result');
109+
90110
if (sudokuIsValid(puzzle)) {
91111
resultElement.textContent = "The puzzle is valid!";
92-
resultElement.className = "result valid";
112+
resultElement.style.color = "green";
93113
} else {
94114
resultElement.textContent = "The puzzle is invalid.";
95-
resultElement.className = "result invalid";
115+
resultElement.style.color = "red";
96116
}
97117
}

0 commit comments

Comments
 (0)