Repciu

Untitled

Jul 2nd, 2024 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Księga ocen</title>
  5. <style>
  6. /* Global styles */
  7. body {
  8. font-family: Arial, sans-serif;
  9. margin: 20px;
  10. }
  11.  
  12. /* Table styles */
  13. table {
  14. border-collapse: collapse;
  15. width: 100%;
  16. border: 1px solid #ddd;
  17. }
  18. th, td {
  19. border: 1px solid #ddd;
  20. padding: 10px;
  21. text-align: left;
  22. }
  23. th {
  24. background-color: #f0f0f0;
  25. }
  26. tr:nth-child(even) {
  27. background-color: #f9f9f9;
  28. }
  29.  
  30. /* Form styles */
  31. form {
  32. margin-bottom: 20px;
  33. }
  34. label {
  35. display: block;
  36. margin-bottom: 10px;
  37. }
  38. input[type="text"], input[type="number"] {
  39. width: 100%;
  40. padding: 10px;
  41. margin-bottom: 20px;
  42. border: 1px solid #ccc;
  43. }
  44. input[type="submit"] {
  45. background-color: #4CAF50;
  46. color: #fff;
  47. padding: 10px 20px;
  48. border: none;
  49. border-radius: 5px;
  50. cursor: pointer;
  51. }
  52. input[type="submit"]:hover {
  53. background-color: #3e8e41;
  54. }
  55.  
  56. /* Header styles */
  57. h1 {
  58. font-size: 24px;
  59. margin-bottom: 10px;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <h1>Księga ocen</h1>
  65. <form id="add-grade-form">
  66. <label for="student-name">Imię i nazwisko ucznia:</label>
  67. <input type="text" id="student-name" name="student-name"><br><br>
  68. <label for="grade">Ocena:</label>
  69. <input type="number" id="grade" name="grade"><br><br>
  70. <input type="submit" value="Dodaj ocenę">
  71. </form>
  72. <table id="grade-table">
  73. <thead>
  74. <tr>
  75. <th>Imię i nazwisko ucznia</th>
  76. <th>Oceny</th>
  77. <th>Średnia ocena</th>
  78. </tr>
  79. </thead>
  80. <tbody id="grade-table-body">
  81. <!-- oceny będą wyświetlane tutaj -->
  82. </tbody>
  83. </table>
  84.  
  85. <script>
  86. // pobierz elementy tabeli i formularza
  87. const table = document.getElementById('grade-table');
  88. const form = document.getElementById('add-grade-form');
  89. const tbody = document.getElementById('grade-table-body');
  90.  
  91. // utwórz obiekt do przechowywania ocen
  92. const grades = {};
  93.  
  94. // dodaj event listener do formularza
  95. form.addEventListener('submit', (e) => {
  96. e.preventDefault();
  97. const studentName = document.getElementById('student-name').value;
  98. const grade = document.getElementById('grade').value;
  99.  
  100. // dodaj ocenę do obiektu
  101. if (!grades[studentName]) {
  102. grades[studentName] = [];
  103. }
  104. grades[studentName].push(Number(grade));
  105.  
  106. // zaktualizuj tabelę
  107. updateTable();
  108. });
  109.  
  110. // funkcja do aktualizacji tabeli
  111. function updateTable() {
  112. tbody.innerHTML = '';
  113. Object.keys(grades).forEach((studentName) => {
  114. const gradesArray = grades[studentName];
  115. const averageGrade = calculateAverage(gradesArray);
  116. const row = document.createElement('tr');
  117. row.innerHTML = `
  118. <td>${studentName}</td>
  119. <td>${gradesArray.join(', ')}</td>
  120. <td>${averageGrade.toFixed(2)}</td>
  121. `;
  122. tbody.appendChild(row);
  123. });
  124. }
  125.  
  126. // funkcja do obliczania średniej oceny
  127. function calculateAverage(gradesArray) {
  128. return gradesArray.reduce((a, b) => a + b, 0) / gradesArray.length;
  129. }
  130. </script>
  131. </body>
  132. </html>
Advertisement
Add Comment
Please, Sign In to add comment