Guest User

Untitled

a guest
Apr 12th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. function solve() {
  2. let rows = document
  3. .getElementsByTagName('tbody')[0]
  4. .getElementsByTagName('tr');
  5. let buttons = document.getElementsByTagName('button');
  6.  
  7. let checkBtn = buttons[0];
  8. let clearBtn = buttons[1];
  9.  
  10. checkBtn.addEventListener('click', () => {
  11. let inputs = Array.from(document.getElementsByTagName('input'));
  12.  
  13. let emptyOrWrongInput = checkIfInputsEmptyOrWrong(inputs);
  14.  
  15. if (emptyOrWrongInput) {
  16. outputNotSolved();
  17. return;
  18. }
  19.  
  20. let rowsSame = checkRows(inputs);
  21. let colsSame = checkCols(inputs);
  22.  
  23. if (rowsSame || colsSame) {
  24. outputNotSolved();
  25. } else {
  26. outputSolved();
  27. }
  28. });
  29.  
  30. clearBtn.addEventListener('click', () => {
  31. for (const row of rows) {
  32. Array.from(row.children).forEach((el) => {
  33. el.children[0].value = '';
  34. });
  35. }
  36. document.getElementsByTagName('table')[0].style.border = '';
  37. document.getElementById('check').children[0].textContent = '';
  38. });
  39.  
  40. function outputNotSolved() {
  41. document.getElementsByTagName('table')[0].style.border = '2px solid red';
  42. document.getElementById('check').children[0].textContent =
  43. 'NOP! You are not done yet...';
  44. }
  45.  
  46. function outputSolved() {
  47. document.getElementsByTagName('table')[0].style.border = '2px solid green';
  48. document.getElementById('check').children[0].textContent =
  49. 'You solve it! Congratulations!';
  50. }
  51.  
  52. function checkIfInputsEmptyOrWrong(inputs) {
  53. for (const input of inputs) {
  54. if (input.value === '') {
  55. return true;
  56. }
  57. let number = +input.value;
  58. if (number < 1 || number > 3) {
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64.  
  65. function checkRows(inputs) {
  66. for (let i = 0; i < inputs.length; i += 3) {
  67. for (let j = i + 1; j < i + 3; j++) {
  68. if ((j + 1) % 3 === 0) {
  69. if (inputs[j - 1].value === inputs[j].value) {
  70. return true;
  71. }
  72. }
  73. if (inputs[i].value === inputs[j].value) {
  74. return true;
  75. }
  76. }
  77. }
  78. return false;
  79. }
  80.  
  81. function checkCols(inputs) {
  82. for (let i = 0; i < inputs.length / 3; i++) {
  83. for (let j = i + 3; j < inputs.length; j += 3) {
  84. if (j >= inputs.length - 3) {
  85. if (inputs[j - 3].value === inputs[j].value) {
  86. return true;
  87. }
  88. }
  89. if (inputs[i].value === inputs[j].value) {
  90. return true;
  91. }
  92. }
  93. }
  94. return false;
  95. }
  96. }
Add Comment
Please, Sign In to add comment