Advertisement
nikolayneykov

Untitled

May 20th, 2019
117
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.   function validateSudoku () {
  12.     let allValues = document.querySelectorAll('input').values
  13.  
  14.     let firstRowInputs = Array.from(
  15.       new Set(Array.from(firstRow.children).map(x => +x.children[0].value))
  16.     )
  17.     let secondRowInputs = Array.from(
  18.       new Set(Array.from(secondRow.children).map(x => +x.children[0].value))
  19.     )
  20.     let thirdRowInputs = Array.from(
  21.       new Set(Array.from(thirdRow.children).map(x => +x.children[0].value))
  22.     )
  23.  
  24.     let firstColum = new Set([
  25.       firstRowInputs[0],
  26.       secondRowInputs[0],
  27.       thirdRowInputs[0]
  28.     ])
  29.     let secondColum = new Set([
  30.       firstRowInputs[1],
  31.       secondRowInputs[1],
  32.       thirdRowInputs[1]
  33.     ])
  34.     let thirdColum = new Set([
  35.       firstRowInputs[2],
  36.       secondRowInputs[2],
  37.       thirdRowInputs[2]
  38.     ])
  39.  
  40.     if (
  41.       firstRowInputs.length === 3 &&
  42.       secondRowInputs.length === 3 &&
  43.       thirdRowInputs.length === 3 &&
  44.       firstColum.size === 3 &&
  45.       secondColum.size === 3 &&
  46.       thirdColum.size === 3
  47.     ) {
  48.       table.style.border = '2px solid green'
  49.       message.textContent = 'You solve it! Congratulations!'
  50.       message.style.color = 'green'
  51.     } else {
  52.       table.style.border = '2px solid red'
  53.       message.textContent = 'NOP! You are not done yet...'
  54.       message.style.color = 'red'
  55.     }
  56.   }
  57.  
  58.   function clearData () {
  59.     Array.from(document.querySelectorAll('tbody tr td input')).forEach(
  60.       d => (d.value = '')
  61.     )
  62.     table.style.border = 'none'
  63.     message.textContent = ''
  64.   }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement