// Function to create an employee record
function createEmployeeRecord(employeeData) {
const [firstName, familyName, title, payPerHour] = employeeData;
return {
firstName,
familyName,
title,
payPerHour,
timeInEvents: [],
timeOutEvents: []
};
}
// Function to create employee records for multiple employees
function createEmployeeRecords(employeesData) {
return employeesData.map(createEmployeeRecord);
}
// Function to record an employee's clock-in time
function createTimeInEvent(employee, timeStamp) {
if (!employee.timeInEvents) employee.timeInEvents = []; // Check if timeInEvents array exists
employee.timeInEvents.push({
type: "TimeIn",
hour: parseInt(timeStamp.slice(11, 13)),
date: timeStamp.slice(0, 10)
});
return employee;
}
// Function to record an employee's clock-out time
function createTimeOutEvent(employeeRecord, dateTime) {
if (!dateTime) return employeeRecord; // Check if dateTime is provided
const [date, hour] = dateTime.split(' ');
employeeRecord.timeOutEvents.push({
type: "TimeOut",
hour: parseInt(hour, 10),
date
});
return employeeRecord;
}
// Function to calculate hours worked by an employee on a specific date
function hoursWorkedOnDate(employeeRecord, date) {
const timeInEvent = employeeRecord.timeInEvents.find(event => event.date === date);
const timeOutEvent = employeeRecord.timeOutEvents.find(event => event.date === date);
return (timeOutEvent.hour - timeInEvent.hour) / 100;
}
// Function to calculate wages earned by an employee on a specific date
function wagesEarnedOnDate(employeeRecord, date) {
const hoursWorked = hoursWorkedOnDate(employeeRecord, date);
return hoursWorked * employeeRecord.payPerHour;
}
// Function to calculate total wages earned by an employee for all dates
function allWagesFor(employeeRecord) {
return employeeRecord.timeInEvents.reduce((totalWages, event) => totalWages + wagesEarnedOnDate(employeeRecord, event.date), 0);
}
// Function to find an employee record by first name
function findEmployeeByFirstName(employeesArray, firstName) {
return employeesArray.find(employee => employee.firstName === firstName);
}
// Function to calculate total payroll for all employees
function calculatePayroll(employeesArray) {
return employeesArray.reduce((totalPayroll, employee) => totalPayroll + allWagesFor(employee), 0);
}