@@ -37,60 +37,16 @@ You may have noticed we've already used `reduce` to `flatten` our arrays.
37
37
38
38
``` js
39
39
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), []);
43
41
};
44
42
```
45
43
46
44
With ` flatten ` , the initialValue was set to an empty array which each value was ` concat ` onto.
47
45
48
46
Do some practice with ` reduce ` , before you use it to narrow down a cheating suspect.
49
47
50
-
51
- + Use ` reduce ` to sum the numbers in the ` practice ` array
48
+ + load suspectData. We will come back to this after some practice;
52
49
@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')
94
50
@action (open('data/suspectData.js'))
95
51
@action (set(
96
52
```
@@ -279,6 +235,46 @@ export default suspectData;
279
235
```
280
236
))
281
237
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 ` ')
282
278
283
279
+ ` reduce ` to an array of suspect scores from the ` suspectData ` we collected previously.
284
280
@test ('07/04')
@@ -287,11 +283,9 @@ export default suspectData;
287
283
```
288
284
289
285
// [{ name: 'suspectName', scores: [ 50, 65, 75, 85...] } ...]
290
- const suspectScores = suspectData.reduce(function (total, next) {
286
+ const suspectScores = suspectData.reduce((total, next) => {
291
287
// 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);
295
289
if (index < 0) {
296
290
total.push({
297
291
::>
@@ -323,7 +317,7 @@ const suspectScores = suspectData.reduce(function(total, next) {
323
317
@action (insert(
324
318
```
325
319
326
- const suspectStats = suspectScores.map(function (suspect) {
320
+ const suspectStats = suspectScores.map((suspect) => {
327
321
// calculate the total difference in scores from the averages
328
322
const difference = suspect.scores.reduce(::>);
329
323
@@ -352,7 +346,7 @@ function isCheater(suspect) {
352
346
}
353
347
354
348
// reduce down to a string of likely suspects
355
- const likelySuspects = suspectStats.reduce(function (::>) {}, []);
349
+ const likelySuspects = suspectStats.reduce((::>) => {}, []);
356
350
```
357
351
))
358
352
@hint ('use ` .join(', ') ` ')
0 commit comments