From 76cde21bb4b2771b603219061c6f953a56dd00b8 Mon Sep 17 00:00:00 2001 From: Wanjiku Kimani Date: Sat, 10 Dec 2022 21:06:25 +0300 Subject: [PATCH] Completed lab --- code_explanation | 9 +++++ index.js | 95 +++++++++++++++++++++++++++++++++++++++++++---- package-lock.json | 8 ++-- 3 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 code_explanation diff --git a/code_explanation b/code_explanation new file mode 100644 index 00000000..57346453 --- /dev/null +++ b/code_explanation @@ -0,0 +1,9 @@ +## createEmployeeRecords +`map()` iterates through the container array + ex employees[ + ["moe", "sizlak", "barkeep", 2], + ["bartholomew", "simpson", "scamp", 3] + ["Mister", "Matt", "Chief Awesomeness Offiser", 1000] + ] + +- calling the createEmployeeRecord converts the nested arrays into objects \ No newline at end of file diff --git a/index.js b/index.js index e303d299..cf5053a5 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,92 @@ /* Your Code Here */ -/* - 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! - - As a result, the lessons for this function will pass *and* it will be available - for you to use if you need it! - */ +//converts an array with employee record to an object +let employee1 = ["Gray", "Worm", "Security", 1] +function createEmployeeRecord(employee){ + // employee= ["moe", "sizlak", "barkeep", 2] + const employeeRecord={ + firstName:employee[0], + familyName:employee[1], + title:employee[2], + payPerHour:employee[3], + timeInEvents:[], + timeOutEvents:[] + } + return employeeRecord; +} +createEmployeeRecord(employee1) + + + +let employees=[ + ["bartholomew", "simpson", "scamp",2], + ["Mister", "Matt", "Chief Awesomeness Offiser",3] +] + +// converts multiple arrays that are nested inside a parent array +//into objects similar to the employeeRecord +const createEmployeeRecords=(employeesContainerArray)=>{ + let employeeRecords=employeesContainerArray.map(employee=>createEmployeeRecord(employee)) + return employeeRecords; +} +createEmployeeRecords(employees) + + + +//"it adds a timeIn event Object to an employee's record of timeInEvents when +//provided an employee record and Date/Time String and returns the updated record" +let createTimeInEvent = function(dateStamp){ + + this.timeInEvents.push({ + type: "TimeIn", + hour: Number.parseInt(dateStamp.slice(11)), + date: dateStamp.slice(0, 10) + }) + + return this +} + +let createTimeOutEvent = function(dateStamp){ + + this.timeOutEvents.push({ + type: "TimeOut", + hour: Number.parseInt(dateStamp.slice(11)), + date: dateStamp.slice(0, 10) + }) + + return this +} + +let hoursWorkedOnDate = function(soughtDate){ + let comingIn = this.timeInEvents.find(function(e){ + return e.date === soughtDate + }) + + let goingOut = this.timeOutEvents.find(function(e){ + return e.date === soughtDate + }) + + return (goingOut.hour - comingIn.hour) / 100 +} + +let wagesEarnedOnDate = function(dateSought){ + let rawWage = hoursWorkedOnDate.call(this, dateSought) + * this.payPerHour + return parseFloat(rawWage.toString()) +} + +let findEmployeeByFirstName = function(srcArray, firstName) { + return srcArray.find(function(rec){ + return rec.firstName === firstName + }) +} + +let calculatePayroll = function(arrayOfEmployeeRecords){ + return arrayOfEmployeeRecords.reduce(function(memo, rec){ + return memo + allWagesFor.call(rec) + }, 0) +} + const allWagesFor = function () { const eligibleDates = this.timeInEvents.map(function (e) { 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": {