Coder Social home page Coder Social logo

learn-co-curriculum / js-advanced-functions-context-lab Goto Github PK

View Code? Open in Web Editor NEW
1.0 22.0 609.0 365 KB

A lab documenting the shift to context-based function calls in a record-oriented programming approach

License: Other

HTML 2.08% JavaScript 97.92%

js-advanced-functions-context-lab's Introduction

JavaScript Advanced Functions: Context Lab

Learning Goals

  • Explicitly override context with call and apply
  • Explicitly lock context for a function with bind

Introduction

In this lab, we're going to build the time-card and payroll application using the record-oriented approach again. This lab will feature the same topic and area of work as the previous lab; however, how we call and use functions will change with our new knowledge. While the code will stay mostly the same, you're going to need to use this a lot more.

The tests guide you to implementing a time card system: when someone enters the company's state of the art technical office, the employee has to insert their card in a time-clock which will record the time they came in. When it's time to leave, the employee will "punch out."

For simplicity's sake, we'll make these assumptions:

  1. Assume that employees always check in and check out
  2. Assume employees always check in and out on the hour
  3. The time is represented on a 24-hour clock (1300 is 1:00 pm); this keeps the math easier and is the standard in most of the world
  4. When timestamps are needed, they will be provided as Strings in the form: "YYYY-MM-DD 800" or "YYYY-MM-DD 1800" e.g. "2018-01-01 2300"
  5. Employees will never work across days i.e. in at 2200 and out at 0400 the next day.

The lab tests will guide you toward a solution. Keep in mind, the goal is to understand how to "grow" an application in "record-oriented" fashion in JavaScript, as well as pass the lab. Make sure you're learning about this app design while you pass the solutions.

As before, if you find yourself having extra time, use the guidance in the previous lab to make your application more robust.

Take advantage of your collection-processing strengths that you trained up over the last few lessons.

Put your code in index.js.

While you will want to be guided by the tests, you will implement the following functions. To make the tests easier to read, we've provided the signatures of the functions.

A function signature is the function name, the arguments it expects, and what the function returns.

createEmployeeRecord

  • Argument(s)
    • A 4-element Array of a String, String, String, and Number corresponding to a first name, family name, title, and pay rate per hour
  • Returns
    • JavaScript Object with keys:
      • firstName
      • familyName
      • title
      • payPerHour
      • timeInEvents
      • timeOutEvents
  • Behavior
    • Loads Array elements into corresponding Object properties. Additionally, initialize empty Arrays on the properties timeInEvents and timeOutEvents.

createEmployeeRecords

  • Argument(s)
    • Array of Arrays
  • Returns
    • Array of Objects
  • Behavior
    • Converts each nested Array into an employee record using createEmployeeRecord and accumulates it to a new Array

createTimeInEvent

  • Argument(s)
  • Returns
    • The record that was just updated
  • Behavior
    • Add an Object with keys:
      • type: Set to "TimeIn"
      • hour: Derived from the argument
      • date: Derived from the argument

createTimeOutEvent

  • Argument(s)
  • Returns
    • The record that was just updated
  • Behavior
    • Add an Object with keys:
      • type: Set to "TimeOut"
      • hour: Derived from the argument
      • date: Derived from the argument

hoursWorkedOnDate

  • Argument(s)
    • A date of the form "YYYY-MM-DD"
  • Returns
    • Hours worked, an Integer
  • Behavior
    • Given a date, find the number of hours elapsed between that date's timeInEvent and timeOutEvent

wagesEarnedOnDate

  • Argument(s)
    • A date of the form "YYYY-MM-DD"
  • Returns
    • Pay owed
  • Behavior
    • Using hoursWorkedOnDate, multiply the hours by the record's payRate to determine amount owed. Amount should be returned as a number.

allWagesFor

  • Argument(s)
    • None
  • Returns
    • Sum of pay owed to one employee for all dates, as a number
  • Behavior
    • Using wagesEarnedOnDate, accumulate the value of all dates worked by the employee in the record used as context. Amount should be returned as a number. HINT: You will need to find the available dates somehow....

findEmployeeByFirstName

  • Argument(s)
    • srcArray: Array of employee records
    • firstName: String representing a first name held in an employee record
  • Returns
    • Matching record or undefined
  • Behavior
    • Test the firstName field for a match with the firstName argument

calculatePayroll

  • Argument(s)
    • Array of employee records
  • Returns
    • Sum of pay owed for all employees for all dates, as a number
  • Behavior
    • Using allWagesFor for each of the employees, accumulate the value of all dates worked by the employee in the record used as context. Amount should be returned as a number.

A Mystery on the Horizon

You'll notice that in this lab we give you the implementation of allWagesFor. As part of writing this challenge, we ran right smack into one of the most famous bugs in JavaScript land: "the lost context bug." Because we've not taught you to deal with it, we've "given" you this function. We think you can solve the other tests with this little piece having been given to you.

If you find yourself having extra time, try researching this topic on your own. We'll tell you all about it in our next lesson, though.

Conclusion

This is one of the hardest topics in JavaScript. But you have hands-on experience with the why and motivations of it! You're so much better off than most JavaScript hackers who never quite get the hang of it. It's been a lot of growth, but this hard-won knowledge is going to help you do staggeringly cool things

Resources

js-advanced-functions-context-lab's People

Contributors

danielatellez avatar dependabot[bot] avatar graciemcguire avatar ihollander avatar maxwellbenton avatar rrcobb avatar sgharms avatar sha-academyxi avatar thuyanduong-flatiron avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-advanced-functions-context-lab's Issues

Test not testing the function it describes

Lines 213 to 244 in indexTest.js refer to a function 'payrollExpense' in it's describe, (side note is not implemented at all in any tests, please check my other issue on this lab), but does not call the function 'payrollExpense' or call the function 'calculatePayroll' that the previous test checks existence of.

What the test does is call different functions (allWagesFor & reduce) to calculate the sum of the payroll. Shouldn't it just use 'calculatePayroll' for this? And wouldn't it this be very similar to the 'calculatePayroll tests'?

IsExternal error message

when running learn on this lab, I continued to get a isExternal error message learn test

[email protected] test
mocha --timeout 30000 -R mocha-multi --reporter-options spec=-,json=.results.json

TypeError: Cannot set property 'isExternal' of undefined
at extend (/home/denise/development/flatiron/labs/js-advanced-functions-context-lab-onl01-seng-pt-070620/node_modules/file-system/vendor/util.js:34:19)
at extend (/home/denise/development/flatiron/labs/js-advanced-functions-context-lab-onl01-seng-pt-070620/node_modules/file-system/vendor/util.js:32:7)
at Object. (/home/denise/development/flatiron/labs/js-advanced-functions-context-lab-onl01-seng-pt-070620/node_modules/file-system/vendor/util.js:41:1)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:997:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object. (/home/denise/development/flatiron/labs/js-advanced-functions-context-lab-onl01-seng-pt-070620/node_modules/file-system/index.js:6:12)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:997:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object. (/home/denise/development/flatiron/labs/js-advanced-functions-context-lab-onl01-seng-pt-070620/test/helpers.js:4:12)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:997:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.exports.requireOrImport (/home/denise/development/flatiron/labs/js-advanced-functions-context-lab-onl01-seng-pt-070620/node_modules/mocha/lib/esm-utils.js:42:12)
at Object.exports.loadFilesAsync (/home/denise/development/flatiron/labs/js-advanced-functions-context-lab-onl01-seng-pt-070620/node_modules/mocha/lib/esm-utils.js:55:34)
at Mocha.loadFilesAsync (/home/denise/development/flatiron/labs/js-advanced-functions-context-lab-onl01-seng-pt-070620/node_modules/mocha/lib/mocha.js:431:19)
at singleRun (/home/denise/development/flatiron/labs/js-advanced-functions-context-lab-onl01-seng-pt-070620/node_modules/mocha/lib/cli/run-helpers.js:125:15)
at exports.runMocha (/home/denise/development/flatiron/labs/js-advanced-functions-context-lab-onl01-seng-pt-070620/node_modules/mocha/lib/cli/run-helpers.js:190:10)
at Object.exports.handler (/home/denise/development/flatiron/labs/js-advanced-functions-context-lab-onl01-seng-pt-070620/node_modules/mocha/lib/cli/run.js:362:11)
at /home/denise/development/flatiron/labs/js-advanced-functions-context-lab-onl01-seng-pt-070620/node_modules/yargs/build/index.cjs:443:71
npm ERR! code 1
npm ERR! path /home/denise/development/flatiron/labs/js-advanced-functions-context-lab-onl01-seng-pt-070620
npm ERR! command failed
npm ERR! command sh -c mocha --timeout 30000 -R mocha-multi --reporter-options spec=-,json=.results.json

npm ERR! A complete log of this run can be found in:
npm ERR! /home/denise/.npm/_logs/2021-03-07T15_57_16_511Z-debug.log
[10:57:16] js-advanced-functions-context-lab-onl01-seng-pt-070620 (master)
// โ™ฅ Traceback (most recent call last):
11: from /home/denise/.rvm/gems/ruby-2.6.1/bin/ruby_executable_hooks:24:in <main>' 10: from /home/denise/.rvm/gems/ruby-2.6.1/bin/ruby_executable_hooks:24:in eval'
9: from /home/denise/.rvm/gems/ruby-2.6.1/bin/learn-test:23:in <main>' 8: from /home/denise/.rvm/gems/ruby-2.6.1/bin/learn-test:23:in load'
7: from /home/denise/.rvm/gems/ruby-2.6.1/gems/learn-test-3.3.1/bin/learn-test:73:in <top (required)>' 6: from /home/denise/.rvm/gems/ruby-2.6.1/gems/learn-test-3.3.1/lib/learn_test/runner.rb:21:in run'
5: from /home/denise/.rvm/gems/ruby-2.6.1/gems/learn-test-3.3.1/lib/learn_test/runner.rb:21:in fork' 4: from /home/denise/.rvm/gems/ruby-2.6.1/gems/learn-test-3.3.1/lib/learn_test/runner.rb:22:in block in run'
3: from /home/denise/.rvm/gems/ruby-2.6.1/gems/learn-test-3.3.1/lib/learn_test/runner.rb:49:in report_and_clean' 2: from /home/denise/.rvm/gems/ruby-2.6.1/gems/learn-test-3.3.1/lib/learn_test/reporter.rb:13:in report'
1: from /home/denise/.rvm/gems/ruby-2.6.1/gems/learn-test-3.3.1/lib/learn_test/reporter.rb:47:in report' /home/denise/.rvm/gems/ruby-2.6.1/gems/learn-test-3.3.1/lib/learn_test/strategies/mocha.rb:44:in results': undefined method `[]' for nil:NilClass (NoMethodError)

Ultimately, I commented out the const fs (line 4), html (line 12), and modified line 23 to just refer to src in the test/helpers.js file to get around the error

createEmployeeRecords

Test file calls the function createEmployeeRecords, which is not mentioned in the readme and causes the tests to fail. I believe this should be createEmployees; I just duplicated that function I had already written and renamed it createEmployeeRecords, which cause the failing tests to pass.

Test has incorrect function name in describe

Lines 209 & 213 in indexText.js refer to the function 'payrollExpense' but line 210 that checks the presence of a different function, 'calculatePayroll'

it("payrollExpense aggregates all the dates' wages and adds them together", function () {
expect(calculatePayroll).to.exist
})
describe("payrollExpense", function () {

Issue with the last test

The test data for "Natalia" on both the time in and time out arrays have the date "2018-01-01" in them twice. This is important to fix because it changes the outcome of the allWagesFor() function, I think the second date is being counted twice by hoursWorkedOnDate(). At least that is the case for my code, the exact opposite happens if hoursWorkedOnDate() uses find() and calculates using the first occurrence twice. Another reason as to why I think this is that the correct total for calculatePayroll() should be 12480, but the test says the amount should be 11880 which is 4 hours of Natalia's pay less than it should be. The same issue persists on the previous lab since they share the same test data.

This lab is really out of order.

And it doesn't do what it claims to, which is to give students practice using call() and apply() and deal with context issues.

Also: in the next iteration of the JS curriculum, there should be more signposting as to when a lab is small-to-medium and directly about that lesson, and when it's a really big lab, like this, or pokemon-teams, or some others. I like this curriculum better than the older one, because the explanations are SO much better (so congrats on that!) and some of the labs are better, but ones like this one still need some change. When I graduate, I'd happily go through them all again and make suggestions. I think the explanations are good, it's just providing more support to students as we go through and learn.

Incorrect camel casing?

Should the function be named findEmployeeByFirstName rather than findEmployeebyFristName as it is currently required in the tests?

Spelling error

Point 2 has a spelling error.
Assume employees always check in and our on the hour

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.