forked from marquez.juan/clase-10-ejercicios-de-repaso
feat: add employee table with average salary calculation
This commit is contained in:
@@ -1 +1,43 @@
|
|||||||
// Agregar aquí el código javascript
|
// Agregar aquí el código javascript
|
||||||
|
const employees = [
|
||||||
|
{ name: "Ana", sector: "Desarrollo", salary: 150000 },
|
||||||
|
{ name: "Luis", sector: "Diseño", salary: 120000 },
|
||||||
|
{ name: "Marta", sector: "Desarrollo", salary: 160000 },
|
||||||
|
{ name: "Carlos", sector: "RRHH", salary: 110000 },
|
||||||
|
{ name: "Julia", sector: "Diseño", salary: 130000 }
|
||||||
|
];
|
||||||
|
|
||||||
|
const tableContainer = document.getElementById('tableEmployees');
|
||||||
|
|
||||||
|
const table = document.createElement('table');
|
||||||
|
const headerRow = document.createElement('tr');
|
||||||
|
|
||||||
|
['Nombre', 'Sector', 'Salario'].forEach(headerText => {
|
||||||
|
const th = document.createElement('th');
|
||||||
|
th.textContent = headerText;
|
||||||
|
headerRow.appendChild(th);
|
||||||
|
});
|
||||||
|
table.appendChild(headerRow);
|
||||||
|
tableContainer.appendChild(table);
|
||||||
|
|
||||||
|
employees.forEach(employee => {
|
||||||
|
const row = document.createElement('tr');
|
||||||
|
['name', 'sector', 'salary'].forEach(key => {
|
||||||
|
const td = document.createElement('td');
|
||||||
|
td.textContent = employee[key];
|
||||||
|
row.appendChild(td);
|
||||||
|
});
|
||||||
|
table.appendChild(row);
|
||||||
|
});
|
||||||
|
|
||||||
|
table.appendChild(document.createElement('tr')); // Fila vacía para separación
|
||||||
|
|
||||||
|
const averageSalary = employees.reduce((sum, emp) => sum + emp.salary, 0) / employees.length;
|
||||||
|
const averageRow = document.createElement('tr');
|
||||||
|
const averageLabelCell = document.createElement('td');
|
||||||
|
averageLabelCell.textContent = 'Salario Promedio';
|
||||||
|
averageRow.appendChild(averageLabelCell);
|
||||||
|
const averageValueCell = document.createElement('td');
|
||||||
|
averageValueCell.textContent = averageSalary;
|
||||||
|
averageRow.appendChild(averageValueCell);
|
||||||
|
table.appendChild(averageRow);
|
||||||
|
|||||||
@@ -8,7 +8,9 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Ejercicio 6</h1>
|
<h1>Ejercicio 6</h1>
|
||||||
|
<div id="tableEmployees">
|
||||||
|
|
||||||
|
</div>
|
||||||
<script src="ejercicio6.js"></script>
|
<script src="ejercicio6.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user