Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 65 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,59 @@
/* Your Code Here */
function createEmployeeRecord(employeeInfo) {
return {
firstName: employeeInfo[0],
familyName: employeeInfo[1],
title: employeeInfo[2],
payPerHour: employeeInfo[3],
timeInEvents: [],
timeOutEvents: []
}
}

function createEmployeeRecords(employeeInfoArray) {
return employeeInfoArray.map(employeeInfo => createEmployeeRecord(employeeInfo))
}

function createTimeInEvent(timePunch) {
this.timeInEvents.push(parseTimePunch(timePunch, "TimeIn"))
return this
}

function createTimeOutEvent(timePunch) {
this.timeOutEvents.push(parseTimePunch(timePunch, "TimeOut"))
return this
}

function parseTimePunch(timePunchString, type) {
const timePunchFormat = /^\d{4}-\d{2}-\d{2} \d{4}$/;
if (!timePunchFormat.test(timePunchString)) {
throw new Error("Time punch format must be 'YYYY-MM-DD HHMM'");
}
return {
type: type,
hour: Number.parseInt(timePunchString.split(' ')[1]),
date: timePunchString.split(' ')[0]
}
}

function getDuration(timeInTime, timeOutTime) {
const timeInHour = Math.floor(timeInTime / 100);
const timeInMinutes = Number.parseInt(timeInTime.toString().slice(-2)) / 60;
const timeOutHour = Math.floor(timeOutTime / 100);
const timeOutMinutes = Number.parseInt(timeOutTime.toString().slice(-2)) / 60;
return (timeOutHour + timeOutMinutes) - (timeInHour + timeInMinutes)
}
function hoursWorkedOnDate(dateWorked) {
const timeInTime = this.timeInEvents.find(event => event.date === dateWorked).hour;
const timeOutTime = this.timeOutEvents.find(event => event.date === dateWorked).hour;
if (!timeInTime || !timeOutTime) throw new Error("Missing punch detected");
return getDuration(timeInTime, timeOutTime)
}

function wagesEarnedOnDate(dateWorked) { return this.payPerHour * hoursWorkedOnDate.call(this, dateWorked) }

function findEmployeeByFirstName(collection, firstNameString) {
return collection.find(record => record.firstName === firstNameString)
}

/*
We're giving you this function. Take a look at it, you might see some usage
Expand All @@ -10,14 +65,16 @@
*/

const allWagesFor = function () {
const eligibleDates = this.timeInEvents.map(function (e) {
return e.date
})
const eligibleDates = this.timeInEvents.map(function (e) {
return e.date
})

const payable = eligibleDates.reduce(function (memo, d) {
return memo + wagesEarnedOnDate.call(this, d)
}.bind(this), 0) // <== Hm, why did we need to add bind() there? We'll discuss soon!
const payable = eligibleDates.reduce(function (memo, d) {
return memo + wagesEarnedOnDate.call(this, d)
}.bind(this), 0) // <== Hm, why did we need to add bind() there? We'll discuss soon!

return payable
return payable
}


function calculatePayroll(employeeRecords) { return employeeRecords.reduce((total, record) => total + allWagesFor.call(record), 0) }
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A JavaScript lab",
"main": "index.js",
"scripts": {
"test": "mocha --timeout 5000 -R mocha-multi --reporter-options spec=-,json=.results.json"
"test": "mocha --timeout 5000 -b -R mocha-multi --reporter-options spec=-,json=.results.json"
},
"author": "flatironschool",
"license": "Included in Repo",
Expand Down