Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- let rows = document
- .getElementsByTagName('tbody')[0]
- .getElementsByTagName('tr');
- let buttons = document.getElementsByTagName('button');
- let checkBtn = buttons[0];
- let clearBtn = buttons[1];
- checkBtn.addEventListener('click', () => {
- let inputs = Array.from(document.getElementsByTagName('input'));
- let emptyOrWrongInput = checkIfInputsEmptyOrWrong(inputs);
- if (emptyOrWrongInput) {
- outputNotSolved();
- return;
- }
- let rowsSame = checkRows(inputs);
- let colsSame = checkCols(inputs);
- if (rowsSame || colsSame) {
- outputNotSolved();
- } else {
- outputSolved();
- }
- });
- clearBtn.addEventListener('click', () => {
- for (const row of rows) {
- Array.from(row.children).forEach((el) => {
- el.children[0].value = '';
- });
- }
- document.getElementsByTagName('table')[0].style.border = '';
- document.getElementById('check').children[0].textContent = '';
- });
- function outputNotSolved() {
- document.getElementsByTagName('table')[0].style.border = '2px solid red';
- document.getElementById('check').children[0].textContent =
- 'NOP! You are not done yet...';
- }
- function outputSolved() {
- document.getElementsByTagName('table')[0].style.border = '2px solid green';
- document.getElementById('check').children[0].textContent =
- 'You solve it! Congratulations!';
- }
- function checkIfInputsEmptyOrWrong(inputs) {
- for (const input of inputs) {
- if (input.value === '') {
- return true;
- }
- let number = +input.value;
- if (number < 1 || number > 3) {
- return true;
- }
- }
- return false;
- }
- function checkRows(inputs) {
- for (let i = 0; i < inputs.length; i += 3) {
- for (let j = i + 1; j < i + 3; j++) {
- if ((j + 1) % 3 === 0) {
- if (inputs[j - 1].value === inputs[j].value) {
- return true;
- }
- }
- if (inputs[i].value === inputs[j].value) {
- return true;
- }
- }
- }
- return false;
- }
- function checkCols(inputs) {
- for (let i = 0; i < inputs.length / 3; i++) {
- for (let j = i + 3; j < inputs.length; j += 3) {
- if (j >= inputs.length - 3) {
- if (inputs[j - 3].value === inputs[j].value) {
- return true;
- }
- }
- if (inputs[i].value === inputs[j].value) {
- return true;
- }
- }
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment