Skip to content

Commit bcdf4a6

Browse files
committed
Update instructions
1 parent f9cc80f commit bcdf4a6

File tree

2 files changed

+108
-70
lines changed

2 files changed

+108
-70
lines changed

README.md

+58-30
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,33 @@ Imagine an exercise routine: every morning, we run 5 miles. But afterwards —
1616
depending on the day — we might lift weights, go for a swim, or run an extra 5
1717
miles.
1818

19-
In programming-speak, we could write out a function for every day (follow along!):
19+
In programming-speak, we could write out a function for every day (follow along
20+
by writing out these examples in a REPL, or in the `index.js` file!):
2021

2122
```js
2223
function Monday() {
23-
console.log('Go for a five-mile run');
24-
console.log('Pump iron');
24+
console.log("Go for a five-mile run");
25+
console.log("Pump iron");
2526
}
2627

2728
function Tuesday() {
28-
console.log('Go for a five-mile run');
29-
console.log('Swim 40 laps');
29+
console.log("Go for a five-mile run");
30+
console.log("Swim 40 laps");
3031
}
3132

3233
function Wednesday() {
33-
console.log('Go for a five-mile run');
34-
console.log('Go for a five-mile run');
34+
console.log("Go for a five-mile run");
35+
console.log("Go for a five-mile run");
3536
}
3637

3738
function Thursday() {
38-
console.log('Go for a five-mile run');
39-
console.log('Pump iron');
39+
console.log("Go for a five-mile run");
40+
console.log("Pump iron");
4041
}
4142

4243
function Friday() {
43-
console.log('Go for a five-mile run');
44-
console.log('Swim 40 laps');
44+
console.log("Go for a five-mile run");
45+
console.log("Swim 40 laps");
4546
}
4647
```
4748

@@ -52,7 +53,7 @@ What if we pull all of our five-mile runs into their own function?
5253

5354
```js
5455
function runFiveMiles() {
55-
console.log('Go for a five-mile run');
56+
console.log("Go for a five-mile run");
5657
}
5758
```
5859

@@ -61,15 +62,15 @@ same thing for lifting weights and swimming:
6162

6263
```js
6364
function liftWeights() {
64-
console.log('Pump iron');
65+
console.log("Pump iron");
6566
}
6667

6768
function swimFortyLaps() {
68-
console.log('Swim 40 laps');
69+
console.log("Swim 40 laps");
6970
}
7071
```
7172

72-
Awesome! We've cut down a little bit more: `Monday()` could now look like
73+
Awesome! We've cut down a little bit more: `Monday()` could now look like:
7374

7475
```js
7576
function Monday() {
@@ -99,12 +100,17 @@ to use this new function we created in our `Monday()` function:
99100
function Monday() {
100101
exerciseRoutine(liftWeights);
101102
}
103+
104+
function exerciseRoutine(postRunActivity) {
105+
runFiveMiles();
106+
postRunActivity();
107+
}
102108
```
103109

104-
Note that we aren't _calling_ `liftWeights`. When we want to pass a function
105-
as a value, we pass it by _reference_ by omitting the parentheses. We're not
106-
running the function at this point. It's up to `exerciseRoutine()` to call the
107-
function when it is needed.
110+
Note that we aren't _calling_ `liftWeights`. When we want to pass a function as
111+
a value, we pass it by _reference_ by omitting the parentheses at the end of the
112+
function. We're not running the function at this point. It's up to
113+
`exerciseRoutine()` to call the function when it is needed.
108114

109115
If we call `Monday()`, we'll see that we run five miles, and then we lift
110116
weights — awesome!
@@ -130,8 +136,8 @@ defined beforehand! We can pass an _anonymous function_ to `exerciseRoutine()`.
130136
To start with, let's use the full function syntax we've come to know and love:
131137

132138
```js
133-
exerciseRoutine(function() {
134-
console.log('Stretch! Work that core!');
139+
exerciseRoutine(function () {
140+
console.log("Stretch! Work that core!");
135141
});
136142

137143
// "Go for a five-mile run"
@@ -142,11 +148,11 @@ We can rewrite this to be more concise by using an arrow function:
142148

143149
```js
144150
exerciseRoutine(() => {
145-
console.log('Stretch! Work that core!');
151+
console.log("Stretch! Work that core!");
146152
});
147153

148154
// Or even shorter:
149-
exerciseRoutine(() => console.log('Stretch! Work that core!'));
155+
exerciseRoutine(() => console.log("Stretch! Work that core!"));
150156
```
151157

152158
Because we only need to use our function this one time, there's no need to give
@@ -170,20 +176,20 @@ function morningRoutine(exercise) {
170176
let breakfast;
171177

172178
if (exercise === liftWeights) {
173-
breakfast = 'protein bar';
179+
breakfast = "protein bar";
174180
} else if (exercise === swimFortyLaps) {
175-
breakfast = 'kale smoothie';
181+
breakfast = "kale smoothie";
176182
} else {
177-
breakfast = 'granola';
183+
breakfast = "granola";
178184
}
179185

180186
exerciseRoutine(exercise);
181187

182188
// we could give this function a name if we wanted to, but since
183189
// it's only available _inside_ morningRoutine(), we don't need to
184-
return function() {
190+
return function () {
185191
console.log(`Nom nom nom, this ${breakfast} is delicious!`);
186-
}
192+
};
187193
}
188194
```
189195

@@ -210,7 +216,29 @@ If you haven't been following along, it's vitally important that you go back and
210216
do so. First-class functions are one of JavaScript's most powerful features, but
211217
it takes some practice for them to sink in.
212218

219+
## Instructions
220+
221+
To get more practice with first-class functions, this lesson has three tests to
222+
pass that require you to write the following functions in the `index.js` file:
223+
224+
- The `receivesAFunction` function should:
225+
226+
- take a _callback function_ as an argument
227+
- call the callback function
228+
- it doesn't matter what this function returns, so long as it calls the
229+
callback function
230+
231+
- The `returnsANamedFunction` function should:
232+
233+
- take no arguments
234+
- return a _named function_
235+
236+
- The `returnsAnAnonymousFunction` function should:
237+
- take no arguments
238+
- return an _anonymous function_
239+
213240
## Resources
214241

215-
- [Wikipedia](https://en.wikipedia.org/wiki/First-class_function): [First-class function](https://en.wikipedia.org/wiki/First-class_function)
216-
- [JavaScript is Sexy](http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/#more-1037): [Higher-order functions](http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/#more-1037)
242+
- [Wikipedia: First-class function](https://en.wikipedia.org/wiki/First-class_function)
243+
- [JavaScript is Sexy: Higher-order functions](http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/#more-1037)
244+
- [MDN Function Expression (named vs anonymous functions)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function)

test/index-test.js

+50-40
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,63 @@
1-
const expect = require('expect')
2-
const fs = require('fs')
3-
const jsdom = require('mocha-jsdom')
4-
const path = require('path')
1+
const expect = require("expect");
2+
const fs = require("fs");
3+
const jsdom = require("mocha-jsdom");
4+
const path = require("path");
55

6-
7-
describe('index', () => {
6+
describe("index", () => {
87
jsdom({
9-
src: fs.readFileSync(path.resolve(__dirname, '..', 'index.js'), 'utf-8')
10-
})
8+
src: fs.readFileSync(path.resolve(__dirname, "..", "index.js"), "utf-8"),
9+
});
1110

12-
describe('receivesAFunction(callback)', () => {
13-
it('receives a function and calls it', () => {
14-
const spy = expect.createSpy()
11+
describe("receivesAFunction(callback)", () => {
12+
it("receives a function and calls it", () => {
13+
// in the context of this test, 'spy' is the callback function
14+
const spy = expect.createSpy();
1515

16-
receivesAFunction(spy)
16+
// your function should take a callback function as an argument
17+
receivesAFunction(spy);
1718

18-
expect(spy).toHaveBeenCalled()
19-
})
20-
})
19+
// and it should call the callback function
20+
expect(spy).toHaveBeenCalled();
21+
});
22+
});
2123

22-
describe('returnsANamedFunction()', () => {
23-
var fn
24+
describe("returnsANamedFunction()", () => {
25+
let fn;
2426

2527
before(() => {
26-
fn = returnsANamedFunction()
27-
})
28+
// before running each test, we call your 'returnsANamedFunction' function
29+
// and save its return value to a variable called 'fn'
30+
fn = returnsANamedFunction();
31+
});
32+
33+
it("returns a function", () => {
34+
expect(fn).toBeA("function");
35+
});
2836

29-
it('returns a function', () => {
30-
expect(fn).toBeA('function')
31-
})
37+
it("returns a named function", () => {
38+
// if you declare a function with the syntax:
39+
// function myFunction() {}
40+
// it will have a name "myFunction" assigned to its name property
3241

33-
it('returns a named function', () => {
34-
expect(fn.name).toNotEqual('')
35-
})
36-
})
42+
// this checks that the function returned from 'returnsANamedFunction'
43+
// has a name assigned
44+
expect(fn.name).toNotEqual("");
45+
});
46+
});
3747

38-
describe('returnsAnAnonymousFunction()', () => {
39-
var fn
48+
describe("returnsAnAnonymousFunction()", () => {
49+
let fn;
4050

4151
before(() => {
42-
fn = returnsAnAnonymousFunction()
43-
})
44-
45-
it('returns a function', () => {
46-
expect(fn).toBeA('function')
47-
})
48-
49-
it('returns an anonymous function', () => {
50-
expect(fn.name).toEqual('')
51-
})
52-
})
53-
})
52+
fn = returnsAnAnonymousFunction();
53+
});
54+
55+
it("returns a function", () => {
56+
expect(fn).toBeA("function");
57+
});
58+
59+
it("returns an anonymous function", () => {
60+
expect(fn.name).toEqual("");
61+
});
62+
});
63+
});

0 commit comments

Comments
 (0)