This repository has been archived on 2023-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
freecodecamp-projects/7-quality-assurance/4-sudoku-solver/public/index.js
CherryKitten 70cee77a95
Add (WIP) Sudoku Solver
Currently contains the Unit Tests, which should already be done correctly.

Functional tests still need to be added.
2022-11-09 22:06:14 +01:00

64 lines
No EOL
1.9 KiB
JavaScript

const textArea = document.getElementById("text-input");
const coordInput = document.getElementById("coord");
const valInput = document.getElementById("val");
const errorMsg = document.getElementById("error");
document.addEventListener("DOMContentLoaded", () => {
textArea.value =
"..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6..";
fillpuzzle(textArea.value);
});
textArea.addEventListener("input", () => {
fillpuzzle(textArea.value);
});
function fillpuzzle(data) {
let len = data.length < 81 ? data.length : 81;
for (let i = 0; i < len; i++) {
let rowLetter = String.fromCharCode('A'.charCodeAt(0) + Math.floor(i / 9));
let col = (i % 9) + 1;
if (!data[i] || data[i] === ".") {
document.getElementsByClassName(rowLetter + col)[0].innerText = " ";
continue;
}
document.getElementsByClassName(rowLetter + col)[0].innerText = data[i];
}
return;
}
async function getSolved() {
const stuff = {"puzzle": textArea.value}
const data = await fetch("/api/solve", {
method: "POST",
headers: {
"Accept": "application/json",
"Content-type": "application/json"
},
body: JSON.stringify(stuff)
})
const parsed = await data.json();
if (parsed.error) {
errorMsg.innerHTML = `<code>${JSON.stringify(parsed, null, 2)}</code>`;
return
}
fillpuzzle(parsed.solution)
}
async function getChecked() {
const stuff = {"puzzle": textArea.value, "coordinate": coordInput.value, "value": valInput.value}
const data = await fetch("/api/check", {
method: "POST",
headers: {
"Accept": "application/json",
"Content-type": "application/json"
},
body: JSON.stringify(stuff)
})
const parsed = await data.json();
errorMsg.innerHTML = `<code>${JSON.stringify(parsed, null, 2)}</code>`;
}
document.getElementById("solve-button").addEventListener("click", getSolved)
document.getElementById("check-button").addEventListener("click", getChecked)