From d4a9ef45b35beffd49f94c395e3f8f435b14945f Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 21 May 2025 12:53:08 -0400 Subject: [PATCH 1/4] Update existing functions to use this --- index.js | 75 ++++++++++++++++++++++++++++++++++++++++++----- package-lock.json | 8 ++--- package.json | 2 +- 3 files changed, 72 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index e303d299f..a27d27aa6 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,61 @@ -/* 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; + debugger + 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 +67,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() { return this.reduce((total, record) => total + allWagesFor(record), 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/package.json b/package.json index dc887287a..d5822374e 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", From bb6af0598cccb96cb8a9856a3dd96a8e9fdf586a Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 21 May 2025 12:56:29 -0400 Subject: [PATCH 2/4] Fix calculatePayroll --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.js b/index.js index a27d27aa6..0caa7564d 100644 --- a/index.js +++ b/index.js @@ -56,7 +56,6 @@ 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 that's new and different. That's because we're avoiding a well-known, but @@ -79,4 +78,4 @@ const allWagesFor = function () { } -function calculatePayroll() { return this.reduce((total, record) => total + allWagesFor(record), 0) } +function calculatePayroll(employeeRecords) { return employeeRecords.reduce((total, record) => total + allWagesFor.call(record), 0) } From b8cb7ec53db3e849387e644a800de88910d8a060 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 21 May 2025 13:00:48 -0400 Subject: [PATCH 3/4] Remove debugger --- index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/index.js b/index.js index 0caa7564d..c4b0988c9 100644 --- a/index.js +++ b/index.js @@ -40,7 +40,6 @@ function getDuration(timeInTime, timeOutTime) { const timeInMinutes = Number.parseInt(timeInTime.toString().slice(-2)) / 60; const timeOutHour = Math.floor(timeOutTime / 100); const timeOutMinutes = Number.parseInt(timeOutTime.toString().slice(-2)) / 60; - debugger return (timeOutHour + timeOutMinutes) - (timeInHour + timeInMinutes) } function hoursWorkedOnDate(dateWorked) { From c4713ff95b602f2d736661227e879dff21226f12 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 21 May 2025 13:08:36 -0400 Subject: [PATCH 4/4] Fix tab sizes --- index.js | 78 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/index.js b/index.js index c4b0988c9..745d835a8 100644 --- a/index.js +++ b/index.js @@ -1,58 +1,58 @@ function createEmployeeRecord(employeeInfo) { - return { - firstName: employeeInfo[0], - familyName: employeeInfo[1], - title: employeeInfo[2], - payPerHour: employeeInfo[3], - timeInEvents: [], - timeOutEvents: [] - } + return { + firstName: employeeInfo[0], + familyName: employeeInfo[1], + title: employeeInfo[2], + payPerHour: employeeInfo[3], + timeInEvents: [], + timeOutEvents: [] + } } function createEmployeeRecords(employeeInfoArray) { - return employeeInfoArray.map(employeeInfo => createEmployeeRecord(employeeInfo)) + return employeeInfoArray.map(employeeInfo => createEmployeeRecord(employeeInfo)) } function createTimeInEvent(timePunch) { - this.timeInEvents.push(parseTimePunch(timePunch, "TimeIn")) - return this + this.timeInEvents.push(parseTimePunch(timePunch, "TimeIn")) + return this } function createTimeOutEvent(timePunch) { - this.timeOutEvents.push(parseTimePunch(timePunch, "TimeOut")) - return this + 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] - } + 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) + 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) + 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) + return collection.find(record => record.firstName === firstNameString) } /* @@ -65,15 +65,15 @@ function findEmployeeByFirstName(collection, firstNameString) { */ 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 }