Advertisement
Lulunga

DOM 10. Sudomu

Oct 21st, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let 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.     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.textContent = '';
  19.     }
  20.  
  21.     function checkResult() {
  22.         let matrix = [
  23.             [inputs[0].value, inputs[1].value, inputs[2].value],
  24.             [inputs[3].value, inputs[4].value, inputs[5].value],
  25.             [inputs[6].value, inputs[7].value, inputs[8].value]
  26.         ];
  27.         isSudomu = true;
  28.         for (let i = 1; i < matrix.length; i++) {
  29.             let row = matrix[i];
  30.             let col = matrix.map(row => row[i]);
  31.             if (col.length != [...new Set(col)].length || row.length != [...new Set(row)].length) {
  32.                 isSudomu = false;
  33.                 break;
  34.             }
  35.         }
  36.         if (isSudomu) {
  37.             table.style.border = '2px solid green';
  38.             checkPar.style.color = 'green';
  39.             checkPar.textContent = 'You solve it! Congratulations!';
  40.         } else {
  41.             table.style.border = '2px solid red';
  42.             checkPar.style.color = 'red';
  43.             checkPar.textContent = 'NOP! You are not done yet...';
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement