Advertisement
Guest User

Untitled

a guest
Oct 8th, 2019
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     document.addEventListener('DOMContentLoaded', () => {
  3.         let inputs = document.querySelectorAll('input');
  4.         const checkBtn = document.querySelector("#exercise > table > tfoot > tr > td > button:nth-child(1)");
  5.         const clear = document.querySelector("#exercise > table > tfoot > tr > td > button:nth-child(2)");
  6.         const table = document.querySelector('table');
  7.         const checkPar = document.querySelector('#check p');
  8.  
  9.         checkBtn.style.cursor = "pointer";
  10.         clear.style.cursor = "pointer";
  11.  
  12.         clear.addEventListener('click', reset);
  13.         checkBtn.addEventListener('click', checkResult);
  14.  
  15.         function reset() {
  16.             inputs.forEach(input => input.value = '');
  17.             table.style.border = "none";
  18.             checkPar.innerHTML = '';
  19.         }
  20.  
  21.         const checkForEquality = arr => arr.every(el => el === arr[0]);
  22.  
  23.         function checkResult() {
  24.             let temp = [];
  25.             let rowsResults = [];
  26.             let colsResults = [];
  27.             let colOne = [];
  28.             let colTwo = [];
  29.             let colThree = [];
  30.  
  31.             inputs.forEach((input) => {
  32.                     if (temp.length >= 3) {
  33.                         rowsResults.push(checkForEquality(temp));
  34.                         colOne.push(temp[0]);
  35.                         colTwo.push(temp[1]);
  36.                         colThree.push(temp[2]);
  37.                         temp.length = 0;
  38.                     }
  39.                     temp.push(input.value);
  40.             });
  41.             rowsResults.push(checkForEquality(temp));
  42.             colOne.push(temp[0]);
  43.             colTwo.push(temp[1]);
  44.             colThree.push(temp[2]);
  45.  
  46.             colsResults.push(checkForEquality(colOne));
  47.             colsResults.push(checkForEquality(colTwo));
  48.             colsResults.push(checkForEquality(colThree));
  49.  
  50.  
  51.             if (checkForEquality(rowsResults) && checkForEquality(colsResults) && rowsResults.includes(false) && colsResults.includes(false)) {
  52.                 table.style.border = "2px solid green";
  53.                 checkPar.style.color = 'green';
  54.                 checkPar.innerHTML = "You solve it! Congratulations!";
  55.             } else {
  56.                 table.style.border = "2px solid red";
  57.                 checkPar.style.color = 'red';
  58.                 checkPar.innerHTML = "NOP! You are not done yet..." ;
  59.             }
  60.  
  61.         }
  62.     });
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement