-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
146 lines (102 loc) · 4.23 KB
/
script.js
File metadata and controls
146 lines (102 loc) · 4.23 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Get a reference to the #add-employees-btn element
const addEmployeesBtn = document.querySelector('#add-employees-btn');
// Collect employee data
const collectEmployees = function() {
// TODO: Get user input to create and return an array of employee objects
const employeesArray = [];
let addMore = true;
//while loop to collect information
while (addMore) {
let firstName = prompt("Enter employee's first name:");
let lastName = prompt("Enter employee's last name:");
let salary = parseFloat(prompt("Enter the employee salary amount:"));
//assigning values to variables inside an object
let employee = {
firstName: firstName,
lastName: lastName,
salary: salary
}
//pushing the variables into an array
employeesArray.push(employee);
//prompt to break the loop
addMore = confirm("Do you want to add another employee?");
}
return employeesArray;
};
// Display the average salary
const displayAverageSalary = function (employeesArray) {
// TODO: Calculate and display the average salary
let totalSalary = 0;
//Display nothing if no input
if (employeesArray.length === 0) {
console.log("No employees to calculate average salary.");
return;
}
for (let i = 0; i < employeesArray.length; i++) {
totalSalary += employeesArray[i].salary;
}
totalSalary = employeesArray.reduce((acc, employee) => acc + (isNaN(employee.salary) ? 0 : employee.salary), 0);
const averageSalary = totalSalary / employeesArray.length;
// Rounding to two decimal places
const averageSalaryWithTwoDecimals = averageSalary.toFixed(2);
console.log(`The average employee salary between our ${employeesArray.length} employee(s) is $${averageSalaryWithTwoDecimals}`);
// let averageSalary = totalSalary / employeesArray.length;
// //console.log(`the calculated average salary is ${averageSalary}`); //displays calculated averageSalary no format. defaults to whole integer if given integer
// console.log(`The average employee salary between our ${employeesArray.length} employee(s) is $${averageSalary.toFixed(2)}`);
// console.log(typeof averageSalary);
};
// Select a random employee
// TODO: Select and display a random employee
const getRandomEmployee = function (employeesArray) {
const randomIndex = Math.floor(Math.random() * employeesArray.length);
const randomEmployee = employeesArray[randomIndex];
console.log(`Congratulations to ${randomEmployee.firstName} ${randomEmployee.lastName}, our random drawing winner!`);
};
/*
====================
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;
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);