diff --git a/index.js b/index.js index e303d299f..f6ad9ebe2 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,53 @@ -/* Your Code Here */ +const createEmployeeRecord = (employee) => { + return { + firstName: employee[0], + familyName: employee[1], + title: employee[2], + payPerHour: parseInt(employee[3], 10), + timeInEvents: [], + timeOutEvents: [] + } +} + +const createEmployeeRecords = (employees) => { + return employees.map(employee => createEmployeeRecord(employee)) +} + +function createTimeInEvent(dateStamp) { + const [date, time] = dateStamp.split(' ') + const checkIn = { + type: 'TimeIn', + hour: parseInt(time, 10), + date + } + + this.timeInEvents.push(checkIn) + + return this +} + +function createTimeOutEvent(dateStamp) { + const [date, time] = dateStamp.split(' ') + const checkOut = { + type: 'TimeOut', + hour: parseInt(time, 10), + date + } -/* - We're giving you this function. Take a look at it, you might see some usage - that's new and different. That's because we're avoiding a well-known, but - sneaky bug that we'll cover in the next few lessons! + this.timeOutEvents.push(checkOut) - As a result, the lessons for this function will pass *and* it will be available - for you to use if you need it! - */ + return this +} + +function hoursWorkedOnDate(dateStamp){ + const checkInTime = this.timeInEvents.find(event => event.date === dateStamp).hour + const checkOutTime = this.timeOutEvents.find(event => event.date === dateStamp).hour + return (checkOutTime - checkInTime) / 100 +} + +function wagesEarnedOnDate(dateStamp){ + return hoursWorkedOnDate.call(this, dateStamp) * this.payPerHour +} const allWagesFor = function () { const eligibleDates = this.timeInEvents.map(function (e) { @@ -21,3 +61,12 @@ const allWagesFor = function () { return payable } +function findEmployeeByFirstName(employeeRecords, firstName){ + return employeeRecords.find(employee => employee.firstName === firstName) +} + +function calculatePayroll(employeeRecords){ + return employeeRecords.reduce((total, employeeRecord) => { + return total + allWagesFor.call(employeeRecord) + }, 0) +} diff --git a/package-lock.json b/package-lock.json index 13ee0eee2..b369d8bdf 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/test/indexTest.js b/test/indexTest.js index 6c5491c0f..a1b901e60 100644 --- a/test/indexTest.js +++ b/test/indexTest.js @@ -340,7 +340,7 @@ describe("The payroll system", function () { timesOutRecordRow[1].forEach(function(timeOutStamp){ createTimeOutEvent.call(rec, timeOutStamp) }) - }) + }) expect(calculatePayroll(employeeRecords)).to.eql(12480) }) })