Skip to content

Commit 7ee37e2

Browse files
committed
tutorial update for mocha 0.10 complete
1 parent bb1c1d4 commit 7ee37e2

File tree

8 files changed

+90
-100
lines changed

8 files changed

+90
-100
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,7 @@ You may have noticed we've already used `reduce` to `flatten` our arrays.
460460

461461
```js
462462
Array.prototype.flatten = function() {
463-
return this.reduce(function(a, b) {
464-
return a.concat(b);
465-
}, []);
463+
return this.reduce((a, b) => a.concat(b), []);
466464
};
467465
```
468466

coderoad.json

Lines changed: 20 additions & 20 deletions
Large diffs are not rendered by default.

tutorial/06/06.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('06 const newSuspects', () => {
1616

1717
});
1818

19-
describe('05 const suspectData', () => {
19+
describe('06 const suspectData', () => {
2020

2121
const suspectData = concat.__get__('suspectData');
2222

tutorial/06/concat.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -795,9 +795,7 @@ import courses from './data/courses2';
795795
796796
// Array.prototype can be used to create new Array methods
797797
Array.prototype.flatten = function() {
798-
return this.reduce(function(a, b) {
799-
return a.concat(b);
800-
}, []);
798+
return this.reduce((a, b) => a.concat(b), []);
801799
};
802800
```
803801
))
@@ -827,8 +825,8 @@ Return the fields:
827825
828826
// map over courses then
829827
// map over students inside of courses
830-
const doubleArray = courses.map(function(course) {
831-
return course.students.map(function(student) {
828+
const doubleArray = courses.map((course) => {
829+
return course.students.map((student) => {
832830
return {
833831
// fill in the fields
834832
title: ::>'',
@@ -855,7 +853,7 @@ const students = doubleArray::>;
855853
))
856854
@hint('call `.flatten()` on `doubleArray`')
857855

858-
+ Use the `suspects` array to `filter` to only "Hack Kerr"'s data
856+
+ Use the `suspects` array to `filter` to only data matching the names in the `suspects` array
859857
@test('06/05')
860858
@action(insert(
861859
```

tutorial/07/01.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
const expect = require('chai').expect;
1+
describe('01 suspectData', () => {
22

3-
const reduce = require('BASE/07-reduce.js');
3+
const suspectData = require('BASE/data/suspectData.js');
44

5-
describe('01 const total', () => {
6-
7-
const total = reduce.__get__('total');
8-
9-
it('should add the numbers up', () => {
10-
expect(total).to.equal(54);
11-
});
5+
it('should be loaded in "data/suspectData.js"', () => {
6+
expect(suspectData).to.be.defined;
7+
});
128

139
});

tutorial/07/02.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
describe('02 const averages', () => {
1+
const expect = require('chai').expect;
22

3-
const averages = reduce.__get__('averages');
3+
const reduce = require('BASE/07-reduce.js');
44

5-
it('should calculate the average of each class', () => {
6-
expect(averages).to.deep.equal([57, 67, 70, 70, 71, 62, 67, 73, 62, 57]);
7-
});
5+
describe('02 const total', () => {
6+
7+
const total = reduce.__get__('total');
8+
9+
it('should add the numbers up', () => {
10+
expect(total).to.equal(54);
11+
});
812

913
});

tutorial/07/03.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
describe('03 suspectData', () => {
1+
describe('03 const averages', () => {
22

3-
const suspectData = require('BASE/data/suspectData.js');
3+
const averages = reduce.__get__('averages');
44

5-
it('should be loaded in "data/courses2.js"', () => {
6-
expect(suspectData).to.be.defined;
5+
it('should calculate the average of each class', () => {
6+
expect(averages).to.deep.equal([57, 67, 70, 70, 71, 62, 67, 73, 62, 57]);
77
});
88

99
});

tutorial/07/reduce.md

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,60 +37,16 @@ You may have noticed we've already used `reduce` to `flatten` our arrays.
3737

3838
```js
3939
Array.prototype.flatten = function() {
40-
return this.reduce(function(a, b) {
41-
return a.concat(b);
42-
}, []);
40+
return this.reduce((a, b) => a.concat(b), []);
4341
};
4442
```
4543

4644
With `flatten`, the initialValue was set to an empty array which each value was `concat` onto.
4745

4846
Do some practice with `reduce`, before you use it to narrow down a cheating suspect.
4947

50-
51-
+ Use `reduce` to sum the numbers in the `practice` array
48+
+ load suspectData. We will come back to this after some practice;
5249
@test('07/01')
53-
@action(open('07-reduce.js'))
54-
@action(set(
55-
```
56-
import courses from './data/courses2';
57-
// Array.reduce(fn(a, b), initialValue)
58-
59-
const practice = [1, 1, 2, 3, 5, 8, 13, 21];
60-
61-
function add(a, b) {
62-
return a + b;
63-
}
64-
65-
// total the numbers using a reduce function
66-
const total = practice.reduce(::>);
67-
```
68-
))
69-
@hint('with only numbers, the initialValue defaults to 0')
70-
@hint('just call `reduce` with `add`')
71-
72-
+ Not all reduce functions are so easy. `reduce` is a little more difficult to master.
73-
74-
`map` over each course and use `reduce` to calculate the class averages for each class. Set `averages` to the resulting array of all class averages.
75-
@test('07/02')
76-
@action(insert(
77-
```
78-
79-
const averages = courses.map(function(course) {
80-
const sum = course.students.reduce(function(total, student) {
81-
::>
82-
83-
});
84-
return Math.round(sum / course.students.length, 0);
85-
});
86-
```
87-
))
88-
@hint('set the initialValue to 0')
89-
@hint('like this: `reduce(function () {}, 0)`')
90-
@hint('return the sum of `student.score` and `total`')
91-
92-
+ load suspectData
93-
@test('07/03')
9450
@action(open('data/suspectData.js'))
9551
@action(set(
9652
```
@@ -279,6 +235,46 @@ export default suspectData;
279235
```
280236
))
281237

238+
+ Use `reduce` to sum the numbers in the `practice` array
239+
@test('07/02')
240+
@action(open('07-reduce.js'))
241+
@action(set(
242+
```
243+
import courses from './data/courses2';
244+
// Array.reduce(fn(a, b), initialValue)
245+
246+
const practice = [1, 1, 2, 3, 5, 8, 13, 21];
247+
248+
function add(a, b) {
249+
return a + b;
250+
}
251+
252+
// total the numbers using a reduce function
253+
const total = practice.reduce(::>);
254+
```
255+
))
256+
@hint('with only numbers, the initialValue defaults to 0')
257+
@hint('just call `reduce` with `add`')
258+
259+
+ Not all reduce functions are so easy. `reduce` is a little more difficult to master.
260+
261+
`map` over each course and use `reduce` to calculate the class averages for each class. Set `averages` to the resulting array of all class averages.
262+
@test('07/03')
263+
@action(insert(
264+
```
265+
266+
const averages = courses.map((course) => {
267+
const sum = course.students.reduce((total, student) => {
268+
::>
269+
270+
});
271+
return Math.round(sum / course.students.length, 0);
272+
});
273+
```
274+
))
275+
@hint('set the initialValue to 0')
276+
@hint('like this: `reduce(function () {}, 0)`')
277+
@hint('return the sum of `student.score` and `total`')
282278

283279
+ `reduce` to an array of suspect scores from the `suspectData` we collected previously.
284280
@test('07/04')
@@ -287,11 +283,9 @@ export default suspectData;
287283
```
288284
289285
// [{ name: 'suspectName', scores: [ 50, 65, 75, 85...] } ...]
290-
const suspectScores = suspectData.reduce(function(total, next) {
286+
const suspectScores = suspectData.reduce((total, next) => {
291287
// see if suspect name has a list yet
292-
const index = total.findIndex(function(suspect) {
293-
return suspect.name === next.name;
294-
});
288+
const index = total.findIndex((suspect) => suspect.name === next.name);
295289
if (index < 0) {
296290
total.push({
297291
::>
@@ -323,7 +317,7 @@ const suspectScores = suspectData.reduce(function(total, next) {
323317
@action(insert(
324318
```
325319
326-
const suspectStats = suspectScores.map(function(suspect) {
320+
const suspectStats = suspectScores.map((suspect) => {
327321
// calculate the total difference in scores from the averages
328322
const difference = suspect.scores.reduce(::>);
329323
@@ -352,7 +346,7 @@ function isCheater(suspect) {
352346
}
353347
354348
// reduce down to a string of likely suspects
355-
const likelySuspects = suspectStats.reduce(function(::>) {}, []);
349+
const likelySuspects = suspectStats.reduce((::>) => {}, []);
356350
```
357351
))
358352
@hint('use `.join(', ')`')

0 commit comments

Comments
 (0)