// Function to create an employee record
function createEmployeeRecord(firstName, familyName, title, payPerHour) {
return {
firstName: firstName,
familyName: familyName,
title: title,
payPerHour: payPerHour,
timeInEvents: [],
timeOutEvents: []
};
}
// Function to create employee records from an array of arrays
function createEmployeeRecords(arrays) {
return arrays.map(array => createEmployeeRecord(...array));
}
// Function to record time in for an employee
function createTimeInEvent(employee, timeStamp) {
employee.timeInEvents.push({
type: "TimeIn",
hour: parseInt(timeStamp.slice(11, 13)),
date: timeStamp.slice(0, 10)
});
return employee;
}
// Function to record time out for an employee
function createTimeOutEvent(employee, timeStamp) {
employee.timeOutEvents.push({
type: "TimeOut",
hour: parseInt(timeStamp.slice(11, 13)),
date: timeStamp.slice(0, 10)
});
return employee;
}
// Function to calculate hours worked on a specific date
function hoursWorkedOnDate(employee, date) {
const timeIn = employee.timeInEvents.find(event => event.date === date);
const timeOut = employee.timeOutEvents.find(event => event.date === date);
return (timeOut.hour - timeIn.hour) / 100;
}
// Function to calculate wages earned on a specific date
function wagesEarnedOnDate(employee, date) {
const hoursWorked = hoursWorkedOnDate(employee, date);
return hoursWorked * employee.payPerHour;
}
// Function to calculate all wages for an employee
function allWagesFor(employee) {
const datesWorked = employee.timeInEvents.map(event => event.date);
return datesWorked.reduce((totalWages, date) => totalWages + wagesEarnedOnDate(employee, date), 0);
}
// Function to find an employee by first name
function findEmployeeByFirstName(srcArray, firstName) {
return srcArray.find(employee => employee.firstName === firstName);
}
// Function to calculate payroll for all employees
function calculatePayroll(employees) {
return employees.reduce((totalPayroll, employee) => totalPayroll + allWagesFor(employee), 0);
}