Advertisement
kstoyanov

10. * Sudomu

Sep 16th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.   const inputs = document.querySelectorAll('input');
  3.   const checkBtn = document.querySelectorAll('button')[0];
  4.   const clear = document.querySelectorAll('button')[1];
  5.  
  6.   const table = document.querySelector('table');
  7.   const checkPar = document.querySelectorAll('#check p')[0];
  8.  
  9.   checkBtn.style.cursor = 'pointer';
  10.   clear.style.cursor = 'pointer';
  11.  
  12.   const reset = () => {
  13.     [...inputs].forEach((input) => {
  14.       input.value = '';
  15.     });
  16.     table.style.border = 'none';
  17.     checkPar.textContent = '';
  18.   };
  19.  
  20.   const checkResult = () => {
  21.     const matrix = [
  22.       [inputs[0].value, inputs[1].value, inputs[2].value],
  23.       [inputs[3].value, inputs[4].value, inputs[5].value],
  24.       [inputs[6].value, inputs[7].value, inputs[8].value],
  25.     ];
  26.  
  27.     let isSudomu = true;
  28.  
  29.     for (let i = 1; i < matrix.length; i++) {
  30.       const row = matrix[i];
  31.       const col = matrix.map((takeRow) => takeRow[i]);
  32.       if (col.length !== [...new Set(col)].length || row.length !== [...new Set(row)].length) {
  33.         isSudomu = false;
  34.         break;
  35.       }
  36.     }
  37.     if (isSudomu) {
  38.       table.style.border = '2px solid green';
  39.       checkPar.style.color = 'green';
  40.       checkPar.textContent = 'You solve it! Congratulations!';
  41.     } else {
  42.       table.style.border = '2px solid red';
  43.       checkPar.style.color = 'red';
  44.       checkPar.textContent = 'NOP! You are not done yet...';
  45.     }
  46.   };
  47.  
  48.  
  49.   clear.addEventListener('click', reset);
  50.   checkBtn.addEventListener('click', checkResult);
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement