-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.js
74 lines (65 loc) · 1.96 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const puzzleBoard = document.querySelector('#puzzle')
const solveButton = document.querySelector('#solve-button')
const explainerDisplay = document.querySelector('#explainer')
const squares = 81
let submission = []
for (let i = 0; i < squares; i++) {
const inputElement = document.createElement('input')
inputElement.setAttribute('type', 'number')
inputElement.setAttribute('min', '0')
inputElement.setAttribute('max', '9')
if (
((i % 9 == 0 || i % 9 == 1 || i % 9 == 2) && i < 21) ||
((i % 9 == 6 || i % 9 == 7 || i % 9 == 8) && i < 27) ||
((i % 9 == 3 || i % 9 == 4 || i % 9 == 5) && (i > 27 && i < 53)) ||
((i % 9 == 0 || i % 9 == 1 || i % 9 == 2) && i > 53) ||
((i % 9 == 6 || i % 9 == 7 || i % 9 == 8) && i > 53)
) {
inputElement.classList.add('odd-section')
}
puzzleBoard.appendChild(inputElement)
}
const joinValues = () => {
const inputs = document.querySelectorAll('input')
inputs.forEach(input => {
if (input.value) {
submission.push(input.value)
} else {
submission.push('.')
}
})
console.log(submission.join(''))
}
const populateValues = (isSolvable, solution) => {
const inputs = document.querySelectorAll('input')
if (isSolvable && solution) {
inputs.forEach((input, i) => {
input.value = solution[i]
})
explainerDisplay.innerHTML = 'This is the answer'
} else {
explainerDisplay.innerHTML = 'This is not solvable'
}
}
const solve = async () => {
joinValues()
const data = { numbers: submission.join('') }
fetch('http://localhost:8000/solve', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data)
populateValues(data.solvable, data.solution)
submission = []
})
.catch((error) => {
console.error('Error:', error)
})
}
solveButton.addEventListener('click', solve)