@@ -37,60 +37,16 @@ You may have noticed we've already used `reduce` to `flatten` our arrays.
3737
3838``` js
3939Array .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
4644With ` flatten ` , the initialValue was set to an empty array which each value was ` concat ` onto.
4745
4846Do 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