Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- document.addEventListener('DOMContentLoaded', () => {
- let inputs = document.querySelectorAll('input');
- const checkBtn = document.querySelector("#exercise > table > tfoot > tr > td > button:nth-child(1)");
- const clear = document.querySelector("#exercise > table > tfoot > tr > td > button:nth-child(2)");
- const table = document.querySelector('table');
- const checkPar = document.querySelector('#check p');
- checkBtn.style.cursor = "pointer";
- clear.style.cursor = "pointer";
- clear.addEventListener('click', reset);
- checkBtn.addEventListener('click', checkResult);
- function reset() {
- inputs.forEach(input => input.value = '');
- table.style.border = "none";
- checkPar.innerHTML = '';
- }
- const checkForEquality = arr => arr.every(el => el === arr[0]);
- function checkResult() {
- let temp = [];
- let rowsResults = [];
- let colsResults = [];
- let colOne = [];
- let colTwo = [];
- let colThree = [];
- inputs.forEach((input) => {
- if (temp.length >= 3) {
- rowsResults.push(checkForEquality(temp));
- colOne.push(temp[0]);
- colTwo.push(temp[1]);
- colThree.push(temp[2]);
- temp.length = 0;
- }
- temp.push(input.value);
- });
- rowsResults.push(checkForEquality(temp));
- colOne.push(temp[0]);
- colTwo.push(temp[1]);
- colThree.push(temp[2]);
- colsResults.push(checkForEquality(colOne));
- colsResults.push(checkForEquality(colTwo));
- colsResults.push(checkForEquality(colThree));
- if (checkForEquality(rowsResults) && checkForEquality(colsResults) && rowsResults.includes(false) && colsResults.includes(false)) {
- table.style.border = "2px solid green";
- checkPar.style.color = 'green';
- checkPar.innerHTML = "You solve it! Congratulations!";
- } else {
- table.style.border = "2px solid red";
- checkPar.style.color = 'red';
- checkPar.innerHTML = "NOP! You are not done yet..." ;
- }
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement