-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
105 lines (80 loc) · 2.78 KB
/
script.js
File metadata and controls
105 lines (80 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Get a reference to the #add-employees-btn element
const addEmployeesBtn = document.querySelector("#add-employees-btn");
// Collect employee data
const collectEmployees = function () {
const inputs = [];
let more = true;
while (more == true) {
const firstName = window.prompt(`Please enter your first name`); // Prompt for first name
const lastName = window.prompt(`Please enter your last name`); // Prompt for last name
const salary = parseInt(window.prompt(`Please enter your Salary`)); // Prompt for salary
inputs.push({ firstName, lastName, salary });
more = window.confirm(`Would you like to add another employee?`);
}
return inputs;
};
// Display the average salary
const displayAverageSalary = function (inputs) {
let sum = 0;
for (let i = 0; i < inputs.length; i++) {
sum += inputs[i].salary;
}
const average = sum / inputs.length;
console.log(
`The average salary of our ${inputs.length} employees is $${average}`
);
};
// Select a random employee
const getRandomEmployee = function (rng) {
const pick = rng[Math.floor(Math.random() * rng.length)];
console.log(pick);
};
/*
====================
STARTER CODE
Do not modify any of the code below this line:
*/
// Display employee data in an HTML table
const displayEmployees = function (employeesArray) {
// Get the employee table
const employeeTable = document.querySelector("#employee-table");
// Clear the employee table
employeeTable.innerHTML = "";
// Loop through the employee data and create a row for each employee
for (let i = 0; i < employeesArray.length; i++) {
const currentEmployee = employeesArray[i];
const newTableRow = document.createElement("tr");
const firstNameCell = document.createElement("td");
firstNameCell.textContent = currentEmployee.firstName;
6;
newTableRow.append(firstNameCell);
const lastNameCell = document.createElement("td");
lastNameCell.textContent = currentEmployee.lastName;
newTableRow.append(lastNameCell);
const salaryCell = document.createElement("td");
// Format the salary as currency
salaryCell.textContent = currentEmployee.salary.toLocaleString("en-US", {
style: "currency",
currency: "USD",
});
newTableRow.append(salaryCell);
employeeTable.append(newTableRow);
}
};
const trackEmployeeData = function () {
const employees = collectEmployees();
console.table(employees);
displayAverageSalary(employees);
console.log("==============================");
getRandomEmployee(employees);
employees.sort(function (a, b) {
if (a.lastName < b.lastName) {
return -1;
} else {
return 1;
}
});
displayEmployees(employees);
};
// Add event listener to 'Add Employees' button
addEmployeesBtn.addEventListener("click", trackEmployeeData);