Advertisement
nikolayneykov

Untitled

May 2nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.   let table = document.querySelector('table');
  3.   let message = document.querySelector('#check p');
  4.  
  5.   let [firstRow, secondRow, thirdRow] = document.querySelectorAll('tbody tr');
  6.   let [quickCheck, clear] = document.querySelectorAll('button');
  7.  
  8.   quickCheck.addEventListener('click', validateSudoku);
  9.   clear.addEventListener('click', clearData);
  10.  
  11.  
  12.   function validateSudoku() {
  13.     let allValues = document.querySelectorAll('input').values;
  14.  
  15.     let firstRowInputs = Array.from(new Set(Array.from(firstRow.children).map(x => +x.children[0].value)));
  16.     let secondRowInputs = Array.from(new Set(Array.from(secondRow.children).map(x => +x.children[0].value)));
  17.     let thirdRowInputs = Array.from(new Set(Array.from(thirdRow.children).map(x => +x.children[0].value)));
  18.  
  19.     let firstColum = new Set([firstRowInputs[0], secondRowInputs[0], thirdRowInputs[0]]);
  20.     let secondColum = new Set([firstRowInputs[1], secondRowInputs[1], thirdRowInputs[1]]);
  21.     let thirdColum = new Set([firstRowInputs[2], secondRowInputs[2], thirdRowInputs[2]]);
  22.  
  23.     if (firstRowInputs.length === 3 && secondRowInputs.length === 3 && thirdRowInputs.length === 3 &&
  24.         firstColum.size === 3 && secondColum.size === 3 && thirdColum.size === 3) {
  25.  
  26.       table.style.border = '2px solid green';
  27.       message.textContent = 'You solve it! Congratulations!';
  28.       message.style.color = 'green';
  29.  
  30.     } else {
  31.       table.style.border = '2px solid darkred';
  32.       message.textContent = 'NOP! You are not done yet...';
  33.       message.style.color = 'darkred';
  34.     }
  35.   }
  36.  
  37.   function clearData() {
  38.     Array.from(document.querySelectorAll('tbody tr td input')).forEach(d => d.value = '');
  39.     table.style.border = 'none';
  40.     message.textContent = '';
  41.   }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement