Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>JS</title>
- <style type="text/css">
- td, th {
- border: 1px solid #000;
- padding: 5px;
- }
- </style>
- </head>
- <body>
- <table>
- <tr>
- <th class="Part1_SSort"> Nazwisko</th>
- <th class="Part1_SSort">Imię</th>
- <th class="Part1_SSort">Płeć</th>
- <th class="Part1_ISort">Punkty</th>
- </tr>
- <tr>
- <td>Kowalski</td>
- <td>Jan</td>
- <td>M</td>
- <td class="pkt">20</td>
- </tr>
- <tr>
- <td>Nowak</td>
- <td>Andrzej</td>
- <td>M</td>
- <td class="pkt">41</td>
- </tr>
- <tr>
- <td>Nowobilski</td>
- <td>Bogdan</td>
- <td>M</td>
- <td class="pkt">29</td>
- </tr>
- <tr>
- <td>Urbaniak</td>
- <td>Aleksandra</td>
- <td>K</td>
- <td class="pkt">26</td>
- </tr>
- <tr>
- <td>Michalski</td>
- <td>Marcin</td>
- <td>M</td>
- <td class="pkt">nb</td>
- </tr>
- <tr>
- <td>Bogun</td>
- <td>Anna</td>
- <td>K</td>
- <td class="pkt">33</td>
- </tr>
- </table>
- <div id="uczestnicy">Liczba uczestników: <b>-</b></div>
- <div id="suma">Suma punktów: <b>-</b></div>
- <div id="naj">Najlepszy uczestnik: <b>-</b></div>
- <button id="przycisk1" >Pokaz</button>
- <hr>
- <div id="uczestnicyk">Liczba uczestniczących kobiet: <b>-</b></div>
- <div id="sumak">Suma punktów kobiety: <b>-</b></div>
- <div id="najk">Najlepszy uczestnik kobieta: <b>-</b></div>
- <button id="przycisk2" >Pokaz</button>
- <hr>
- <div id="uczestnicym">Liczba uczestniczących mężczyzn: <b>-</b></div>
- <div id="sumam">Suma punktów mężczyzn: <b>-</b></div>
- <div id="najm">Najlepszy uczestnik mężczyzna: <b>-</b></div>
- <button id="przycisk3" >Pokaz</button>
- <script>
- document.addEventListener("DOMContentLoaded", function() {
- function IntegerSort(a,b){return parseInt(a)>parseInt(b);}
- function ValueSort(a,b){return a>b;}
- function Contains(classArray,value){
- for (var i=0; i<classArray.length;i++)
- if (classArray[i]===value) return true;
- return false;
- }
- function attachSorting(){
- var handlers=[['Part1_SSort', ValueSort],['Part1_ISort',IntegerSort]];
- for(var i=0, ths=document.getElementsByTagName('th'); th=ths[i]; i++){
- for (var h=0; h<handlers.length;h++) {
- if(Contains(th.className.split(' '), handlers[h][0])){
- th.columnIndex=i;
- th.order = -1;
- th.sortHandler = handlers[h][1];
- th.onclick=function(){sort(this);}
- }
- }
- }
- }
- function sort(header){
- header.order *= -1;
- var table = header.parentNode.parentNode;
- for (var i=0, th, ths=table.getElementsByTagName('th'); th=ths[i]; i++)
- if (th!=header) th.order = -1;
- var rows=table.getElementsByTagName('tr');
- for(var i=1, tempRows=[], tr; tr=rows[i]; i++){tempRows[i-1]=tr}
- tempRows.sort(function(a,b){
- return header.order*
- (header.sortHandler(
- a.getElementsByTagName('td')[header.columnIndex].innerHTML,
- b.getElementsByTagName('td')[header.columnIndex].innerHTML)?1:-1)});
- for(var i=0; i<tempRows.length; i++){
- table.appendChild(tempRows[i]);
- }
- }
- attachSorting();
- // Wasz kod JS
- document.getElementById("przycisk1").onclick = function() {myFunction1()};
- document.getElementById("przycisk2").onclick = function() {myFunction2()};
- document.getElementById("przycisk3").onclick = function() {myFunction3()};
- const wiersz = document.querySelectorAll('tr');
- const uczestnicy = [];
- const nazwiska = [];
- const puczenstnikow = [];
- const wimiona = [];
- const plecw = [];
- let sum = 0;
- let sumak = 0;
- let sumam = 0;
- let lpkobiet = 0;
- let lpmezczyzn = 0;
- let najkobieta;
- let najmezczyzna;
- pierwszaK = false;
- pierwszyM = false;
- for (let i =1;i<wiersz.length;i++)
- {
- punkty = wiersz[i].querySelector('td.pkt').innerText;
- imiona = wiersz[i].querySelector('td:nth-child(2)').innerText;
- nazwisko = wiersz[i].querySelector('td:nth-child(1)').innerText;
- plec = wiersz[i].querySelector('td:nth-child(3)').innerText;
- punkty = parseInt(punkty);
- nazwiska.push(nazwisko);
- puczenstnikow.push(punkty);
- wimiona.push(imiona);
- plecw.push(plec);
- }
- if (nazwiska.length == wimiona.length && wimiona.length == plecw.length && plecw.length == puczenstnikow.length)
- {
- for(var x = 0;x<puczenstnikow.length;x++)
- {
- if (isNaN(puczenstnikow[x]) == false)
- {
- sum += puczenstnikow[x];
- uczestnicy.push([nazwiska[x] + " " + wimiona[x],plecw[x],puczenstnikow[x]]);
- }
- }
- function sortowanie(x, y)
- {
- if (x[2] > y[2]) return -1;
- return 0;
- }
- uczestnicy.sort(sortowanie);
- for(let x=0;x<uczestnicy.length;x++)
- {
- if (uczestnicy[x][1] === 'K')
- {
- sumak++;
- lpkobiet += uczestnicy[x][2];
- if( pierwszaK == false )
- {
- najkobieta = uczestnicy[x][0]+" "+uczestnicy[x][2];
- pierwszaK = true;
- }
- }
- else
- {
- sumam++;
- lpmezczyzn += uczestnicy[x][2];
- if( pierwszyM == false )
- {
- najmezczyzna = uczestnicy[x][0]+" " + uczestnicy[x][2];
- pierwszyM = true;
- }
- }
- }
- najuczestnik = uczestnicy[0][0] + " " + uczestnicy[0][2];
- function myFunction1() {
- liczbauczestnikow = document.getElementById('uczestnicy').querySelector('b').innerText = uczestnicy.length;
- sum = document.getElementById('suma').querySelector('b').innerText =sum;
- najuczestnik = document.getElementById('naj').querySelector('b').innerText =najuczestnik;
- }
- function myFunction2() {
- sumak = document.getElementById('uczestnicyk').querySelector('b').innerText =sumak;
- lpk = document.getElementById('sumak').querySelector('b').innerText = lpkobiet;
- pk = document.getElementById('najk').querySelector('b').innerText = najkobieta;
- }
- function myFunction3() {
- sumam = document.getElementById('uczestnicym').querySelector('b').innerText = sumam;
- lpm = document.getElementById('sumam').querySelector('b').innerText =lpmezczyzn;
- pm = document.getElementById('najm').querySelector('b').innerText =najmezczyzna;
- }
- }
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement