Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JS</title>
  6. <style type="text/css">
  7. td, th {
  8. border: 1px solid #000;
  9. padding: 5px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14.  
  15. <table>
  16. <tr>
  17. <th class="Part1_SSort"> Nazwisko</th>
  18. <th class="Part1_SSort">Imię</th>
  19. <th class="Part1_SSort">Płeć</th>
  20. <th class="Part1_ISort">Punkty</th>
  21. </tr>
  22. <tr>
  23. <td>Kowalski</td>
  24. <td>Jan</td>
  25. <td>M</td>
  26. <td class="pkt">20</td>
  27. </tr>
  28. <tr>
  29. <td>Nowak</td>
  30. <td>Andrzej</td>
  31. <td>M</td>
  32. <td class="pkt">41</td>
  33. </tr>
  34. <tr>
  35. <td>Nowobilski</td>
  36. <td>Bogdan</td>
  37. <td>M</td>
  38. <td class="pkt">29</td>
  39. </tr>
  40. <tr>
  41. <td>Urbaniak</td>
  42. <td>Aleksandra</td>
  43. <td>K</td>
  44. <td class="pkt">26</td>
  45. </tr>
  46. <tr>
  47. <td>Michalski</td>
  48. <td>Marcin</td>
  49. <td>M</td>
  50. <td class="pkt">nb</td>
  51. </tr>
  52. <tr>
  53. <td>Bogun</td>
  54. <td>Anna</td>
  55. <td>K</td>
  56. <td class="pkt">33</td>
  57. </tr>
  58. </table>
  59. <div id="uczestnicy">Liczba uczestników: <b>-</b></div>
  60. <div id="suma">Suma punktów: <b>-</b></div>
  61. <div id="naj">Najlepszy uczestnik: <b>-</b></div>
  62. <button id="przycisk1" >Pokaz</button>
  63.  
  64. <hr>
  65. <div id="uczestnicyk">Liczba uczestniczących kobiet: <b>-</b></div>
  66. <div id="sumak">Suma punktów kobiety: <b>-</b></div>
  67. <div id="najk">Najlepszy uczestnik kobieta: <b>-</b></div>
  68. <button id="przycisk2" >Pokaz</button>
  69. <hr>
  70. <div id="uczestnicym">Liczba uczestniczących mężczyzn: <b>-</b></div>
  71. <div id="sumam">Suma punktów mężczyzn: <b>-</b></div>
  72. <div id="najm">Najlepszy uczestnik mężczyzna: <b>-</b></div>
  73. <button id="przycisk3" >Pokaz</button>
  74. <script>
  75.  
  76. document.addEventListener("DOMContentLoaded", function() {
  77.  
  78.  
  79.  
  80.  
  81. function IntegerSort(a,b){return parseInt(a)>parseInt(b);}
  82. function ValueSort(a,b){return a>b;}
  83. function Contains(classArray,value){
  84. for (var i=0; i<classArray.length;i++)
  85. if (classArray[i]===value) return true;
  86. return false;
  87. }
  88. function attachSorting(){
  89. var handlers=[['Part1_SSort', ValueSort],['Part1_ISort',IntegerSort]];
  90. for(var i=0, ths=document.getElementsByTagName('th'); th=ths[i]; i++){
  91. for (var h=0; h<handlers.length;h++) {
  92. if(Contains(th.className.split(' '), handlers[h][0])){
  93. th.columnIndex=i;
  94. th.order = -1;
  95. th.sortHandler = handlers[h][1];
  96. th.onclick=function(){sort(this);}
  97. }
  98. }
  99. }
  100. }
  101. function sort(header){
  102. header.order *= -1;
  103. var table = header.parentNode.parentNode;
  104. for (var i=0, th, ths=table.getElementsByTagName('th'); th=ths[i]; i++)
  105. if (th!=header) th.order = -1;
  106. var rows=table.getElementsByTagName('tr');
  107. for(var i=1, tempRows=[], tr; tr=rows[i]; i++){tempRows[i-1]=tr}
  108. tempRows.sort(function(a,b){
  109. return header.order*
  110. (header.sortHandler(
  111. a.getElementsByTagName('td')[header.columnIndex].innerHTML,
  112. b.getElementsByTagName('td')[header.columnIndex].innerHTML)?1:-1)});
  113. for(var i=0; i<tempRows.length; i++){
  114. table.appendChild(tempRows[i]);
  115. }
  116. }
  117. attachSorting();
  118.  
  119. // Wasz kod JS
  120. document.getElementById("przycisk1").onclick = function() {myFunction1()};
  121. document.getElementById("przycisk2").onclick = function() {myFunction2()};
  122. document.getElementById("przycisk3").onclick = function() {myFunction3()};
  123.  
  124.  
  125.  
  126.  
  127. const wiersz = document.querySelectorAll('tr');
  128. const uczestnicy = [];
  129. const nazwiska = [];
  130. const puczenstnikow = [];
  131. const wimiona = [];
  132. const plecw = [];
  133. let sum = 0;
  134. let sumak = 0;
  135. let sumam = 0;
  136. let lpkobiet = 0;
  137. let lpmezczyzn = 0;
  138. let najkobieta;
  139. let najmezczyzna;
  140. pierwszaK = false;
  141. pierwszyM = false;
  142.  
  143. for (let i =1;i<wiersz.length;i++)
  144. {
  145. punkty = wiersz[i].querySelector('td.pkt').innerText;
  146. imiona = wiersz[i].querySelector('td:nth-child(2)').innerText;
  147. nazwisko = wiersz[i].querySelector('td:nth-child(1)').innerText;
  148. plec = wiersz[i].querySelector('td:nth-child(3)').innerText;
  149. punkty = parseInt(punkty);
  150. nazwiska.push(nazwisko);
  151. puczenstnikow.push(punkty);
  152. wimiona.push(imiona);
  153. plecw.push(plec);
  154. }
  155.  
  156. if (nazwiska.length == wimiona.length && wimiona.length == plecw.length && plecw.length == puczenstnikow.length)
  157. {
  158. for(var x = 0;x<puczenstnikow.length;x++)
  159. {
  160. if (isNaN(puczenstnikow[x]) == false)
  161. {
  162. sum += puczenstnikow[x];
  163. uczestnicy.push([nazwiska[x] + " " + wimiona[x],plecw[x],puczenstnikow[x]]);
  164. }
  165. }
  166. function sortowanie(x, y)
  167. {
  168. if (x[2] > y[2]) return -1;
  169. return 0;
  170. }
  171.  
  172. uczestnicy.sort(sortowanie);
  173.  
  174. for(let x=0;x<uczestnicy.length;x++)
  175. {
  176. if (uczestnicy[x][1] === 'K')
  177. {
  178. sumak++;
  179. lpkobiet += uczestnicy[x][2];
  180. if( pierwszaK == false )
  181. {
  182. najkobieta = uczestnicy[x][0]+" "+uczestnicy[x][2];
  183. pierwszaK = true;
  184. }
  185. }
  186. else
  187. {
  188. sumam++;
  189. lpmezczyzn += uczestnicy[x][2];
  190. if( pierwszyM == false )
  191. {
  192. najmezczyzna = uczestnicy[x][0]+" " + uczestnicy[x][2];
  193. pierwszyM = true;
  194. }
  195. }
  196. }
  197.  
  198. najuczestnik = uczestnicy[0][0] + " " + uczestnicy[0][2];
  199.  
  200.  
  201. function myFunction1() {
  202. liczbauczestnikow = document.getElementById('uczestnicy').querySelector('b').innerText = uczestnicy.length;
  203. sum = document.getElementById('suma').querySelector('b').innerText =sum;
  204. najuczestnik = document.getElementById('naj').querySelector('b').innerText =najuczestnik;
  205. }
  206.  
  207.  
  208. function myFunction2() {
  209. sumak = document.getElementById('uczestnicyk').querySelector('b').innerText =sumak;
  210. lpk = document.getElementById('sumak').querySelector('b').innerText = lpkobiet;
  211. pk = document.getElementById('najk').querySelector('b').innerText = najkobieta;
  212. }
  213.  
  214.  
  215.  
  216. function myFunction3() {
  217. sumam = document.getElementById('uczestnicym').querySelector('b').innerText = sumam;
  218. lpm = document.getElementById('sumam').querySelector('b').innerText =lpmezczyzn;
  219. pm = document.getElementById('najm').querySelector('b').innerText =najmezczyzna;
  220. }
  221.  
  222. }
  223. });
  224. </script>
  225. </body>
  226. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement