From beef35ea25632a2013f3faf7c7bed3587f40f70f Mon Sep 17 00:00:00 2001 From: Yuriy Ivanenko Date: Tue, 21 May 2024 13:54:50 -0400 Subject: [PATCH] Rewrite Context Lab using this keyword --- index.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index e303d299f..7708b2df8 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,55 @@ /* Your Code Here */ +const createEmployeeRecord = (employee) => { + return { + firstName: employee[0], + familyName: employee[1], + title: employee[2], + payPerHour: employee[3], + timeInEvents: [], + timeOutEvents: [], + } +} + +const createEmployeeRecords = (records) => records.map(record => createEmployeeRecord(record)) + +const createTimeInEvent = function(dateStamp){ + this.timeInEvents.push({ + type: "TimeIn", + date: dateStamp.slice(0,10), + hour: parseInt(dateStamp.slice(-4)) + }) + return this +} + +const createTimeOutEvent = function(dateStamp){ + this.timeOutEvents.push({ + type: "TimeOut", + date: dateStamp.slice(0,10), + hour: parseInt(dateStamp.slice(-4)) + }) + return this +} + +const hoursWorkedOnDate = function(date) { + const timeIn = this.timeInEvents.find(event => event.date === date).hour + const timeOut = this.timeOutEvents.find(event => event.date === date).hour + return (timeOut - timeIn ) / 100 +} + +const wagesEarnedOnDate = function(date){ + const hoursWorked = hoursWorkedOnDate.call(this, date) + return hoursWorked * this.payPerHour +} + +const findEmployeeByFirstName = function(emps, empToFind){ + return emps.find(emp => emp.firstName === empToFind) +} + +function calculatePayroll(employeeRecords){ + return employeeRecords.map(emp => allWagesFor.call(emp)).reduce((acc, wages) => acc + wages) +} + /* 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 @@ -9,7 +59,8 @@ for you to use if you need it! */ -const allWagesFor = function () { +function allWagesFor() { + // console.log(this) const eligibleDates = this.timeInEvents.map(function (e) { return e.date }) @@ -20,4 +71,3 @@ const allWagesFor = function () { return payable } -