Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Księga ocen</title>
- <style>
- /* Global styles */
- body {
- font-family: Arial, sans-serif;
- margin: 20px;
- }
- /* Table styles */
- table {
- border-collapse: collapse;
- width: 100%;
- border: 1px solid #ddd;
- }
- th, td {
- border: 1px solid #ddd;
- padding: 10px;
- text-align: left;
- }
- th {
- background-color: #f0f0f0;
- }
- tr:nth-child(even) {
- background-color: #f9f9f9;
- }
- /* Form styles */
- form {
- margin-bottom: 20px;
- }
- label {
- display: block;
- margin-bottom: 10px;
- }
- input[type="text"], input[type="number"] {
- width: 100%;
- padding: 10px;
- margin-bottom: 20px;
- border: 1px solid #ccc;
- }
- input[type="submit"] {
- background-color: #4CAF50;
- color: #fff;
- padding: 10px 20px;
- border: none;
- border-radius: 5px;
- cursor: pointer;
- }
- input[type="submit"]:hover {
- background-color: #3e8e41;
- }
- /* Header styles */
- h1 {
- font-size: 24px;
- margin-bottom: 10px;
- }
- </style>
- </head>
- <body>
- <h1>Księga ocen</h1>
- <form id="add-grade-form">
- <label for="student-name">Imię i nazwisko ucznia:</label>
- <input type="text" id="student-name" name="student-name"><br><br>
- <label for="grade">Ocena:</label>
- <input type="number" id="grade" name="grade"><br><br>
- <input type="submit" value="Dodaj ocenę">
- </form>
- <table id="grade-table">
- <thead>
- <tr>
- <th>Imię i nazwisko ucznia</th>
- <th>Oceny</th>
- <th>Średnia ocena</th>
- </tr>
- </thead>
- <tbody id="grade-table-body">
- <!-- oceny będą wyświetlane tutaj -->
- </tbody>
- </table>
- <script>
- // pobierz elementy tabeli i formularza
- const table = document.getElementById('grade-table');
- const form = document.getElementById('add-grade-form');
- const tbody = document.getElementById('grade-table-body');
- // utwórz obiekt do przechowywania ocen
- const grades = {};
- // dodaj event listener do formularza
- form.addEventListener('submit', (e) => {
- e.preventDefault();
- const studentName = document.getElementById('student-name').value;
- const grade = document.getElementById('grade').value;
- // dodaj ocenę do obiektu
- if (!grades[studentName]) {
- grades[studentName] = [];
- }
- grades[studentName].push(Number(grade));
- // zaktualizuj tabelę
- updateTable();
- });
- // funkcja do aktualizacji tabeli
- function updateTable() {
- tbody.innerHTML = '';
- Object.keys(grades).forEach((studentName) => {
- const gradesArray = grades[studentName];
- const averageGrade = calculateAverage(gradesArray);
- const row = document.createElement('tr');
- row.innerHTML = `
- <td>${studentName}</td>
- <td>${gradesArray.join(', ')}</td>
- <td>${averageGrade.toFixed(2)}</td>
- `;
- tbody.appendChild(row);
- });
- }
- // funkcja do obliczania średniej oceny
- function calculateAverage(gradesArray) {
- return gradesArray.reduce((a, b) => a + b, 0) / gradesArray.length;
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment