Skip to content

Commit

Permalink
Merge pull request learn-co-curriculum#1 from learn-co-curriculum/wip-rd
Browse files Browse the repository at this point in the history
Wip rd
  • Loading branch information
Robert W Cobb committed May 16, 2019
2 parents ace45c8 + d28adb6 commit 3bbb9cb
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ jspm_packages
!/log/.keep
/tmp

.vscode
65 changes: 60 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,71 @@
# Title
# JavaScript Advanced Functions: Context Lab

## Learning Goals

-SWBAT 1
-SWBAT 2
* Explicitly override context with `call` and `apply`

## Introduction

## SWBAT 1
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.

## SWBAT 2
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 our 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`.

## 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

* [`bind`][bind]
* [`call`][call]
* [`apply`][apply]

[bind]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind
[call]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/call
[apply]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/apply
25 changes: 21 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
const testVar = {}
/* Your Code Here */

function testFunc() {
return "hi"
}
/*
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!
*/

let allWagesFor = function () {
let eligibleDates = this.timeInEvents.map(function (e) {
return e.date
})

let 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
}
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
"mocha": "5.2.0",
"mocha-jsdom": "~1.1.0",
"mocha-multi": "1.0.1"
},
"dependencies": {
"chai-spies": "^1.0.0"
}
}
4 changes: 4 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const chai = require('chai')
global.expect = chai.expect
global.chai = chai
const fs = require('file-system')
const jsdom = require('mocha-jsdom')
const path = require('path')
const babel = require('babel-core');
const spies = require('chai-spies')

chai.use( spies );

const html = fs.readFileSync(path.resolve(__dirname, '..', 'index.html'), 'utf-8')

Expand Down
Loading

0 comments on commit 3bbb9cb

Please sign in to comment.