diff --git a/index.js b/index.js index e303d299..745d835a 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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) } diff --git a/package-lock.json b/package-lock.json index 13ee0eee..b369d8bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3177,7 +3177,7 @@ "dependencies": { "combined-stream": { "version": "1.0.6", - "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.6.tgz", + "resolved": "http://registry.npmjs.org/combined-stream/-/combined-stream-1.0.6.tgz", "integrity": "sha1-cj599ugBrFYTETp+RFqbactjKBg=", "dev": true, "requires": { @@ -3442,9 +3442,9 @@ } }, "lodash": { - "version": "4.17.15", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", - "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", + "version": "4.17.11", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", + "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==", "dev": true }, "lodash.once": { diff --git a/package.json b/package.json index dc887287..d5822374 100644 --- a/package.json +++ b/package.json @@ -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",