You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
-48Lines changed: 0 additions & 48 deletions
Original file line number
Diff line number
Diff line change
@@ -415,51 +415,3 @@ Back to business.
415
415
We have a suspect in mind: a classmate named "Hack Kerr". He's a nice guy, and he's always been friendly to you - but there's something suspicious about him: his name.
416
416
417
417
We'll test out flatten, then re-create our student array of data from the original course data.
418
-
419
-
##### reduce
420
-
421
-
Array -> anything
422
-
423
-
We know our likely suspect is also in the school computer system. Perhaps our suspect also changed his grades.
424
-
425
-
You can't be sure who is a cheater, but you can assume if the grades are well above the average, the person is likely to be the culprit. For this, we'll have to do some basic statistical calculations. We'll need a new tool for transforming arrays into different data representations.
426
-
427
-
`map` has a major limitation: it will always output the same number of elements as the input array.
428
-
429
-
When you want to transform data into something different, you'll likely want to use `reduce`.
Copy file name to clipboardExpand all lines: coderoad.json
+10-63Lines changed: 10 additions & 63 deletions
Original file line number
Diff line number
Diff line change
@@ -72,7 +72,7 @@
72
72
],
73
73
"actions": [
74
74
"open('01-filter.js')",
75
-
"set('const students = require('./data/students').default;\n// Array.filter(fn)\n\nfunction isAda(student) {\n // return true if student name\n // matches \"Ada Lovelace\"\n ::>\n}\n')"
75
+
"set('import students from './data/students';\n// Array.filter(fn)\n\nfunction isAda(student) {\n // return true if student name\n // matches \"Ada Lovelace\"\n ::>\n}\n')"
76
76
],
77
77
"hints": [
78
78
"Some tasks have hints",
@@ -139,7 +139,7 @@
139
139
],
140
140
"actions": [
141
141
"open('02-sort.js')",
142
-
"set('const myBest = require('./data/myBest').default;\n// Array.sort(fn)\n\nfunction compareScore(a, b) {\n switch (true) {\n case b.score > a.score:\n // it should return 1 if b's score is more than a's\n return ::>\n case 'set condition here':\n // it should return -1 if b's score is less than a's\n\n default:\n // it should return 0 if b and a have the same score\n\n }\n}\n')"
142
+
"set('import myBest from './data/myBest';\n// Array.sort(fn)\n\nfunction compareScore(a, b) {\n switch (true) {\n case b.score > a.score:\n // it should return 1 if b's score is more than a's\n return ::>\n case 'set condition here':\n // it should return -1 if b's score is less than a's\n\n default:\n // it should return 0 if b and a have the same score\n\n }\n}\n')"
143
143
]
144
144
},
145
145
{
@@ -196,7 +196,7 @@
196
196
],
197
197
"actions": [
198
198
"open('03-map.js')",
199
-
"set('const myCourses = require('./data/myCourses').default;\n// Array.map(fn)\n\n/*\n * change any the `course.grade` into an 'A'\n *\n * for example:\n * changeGrade({ grade: 'F' }) === { grade: 'A' };\n*/\n\nfunction changeGrade(course) {\n ::>\n}\n\n')"
199
+
"set('import myCourses from './data/myCourses';\n// Array.map(fn)\n\n/*\n * change any the `course.grade` into an 'A'\n *\n * for example:\n * changeGrade({ grade: 'F' }) === { grade: 'A' };\n*/\n\nfunction changeGrade(course) {\n ::>\n}\n\n')"
200
200
],
201
201
"hints": [
202
202
"give `changeGrade` a parameter, call it \"course\"",
@@ -292,7 +292,7 @@
292
292
],
293
293
"actions": [
294
294
"open('04-forEach.js')",
295
-
"set('const myFixed = require('./data/myFixed').default;\n// Array.forEach(fn)\n\nfunction logCourse(course) {\n console.log(`${course.grade} ${course.score} ${course.title}`);\n}\n\n// log your grades to the console\nmyFixed.forEach(::>);\n')"
295
+
"set('import myFixed from './data/myFixed';\n// Array.forEach(fn)\n\nfunction logCourse(course) {\n console.log(`${course.grade} ${course.score} ${course.title}`);\n}\n\n// log your grades to the console\nmyFixed.forEach(::>);\n')"
296
296
],
297
297
"hints": [
298
298
"call `forEach` with `logCourse`"
@@ -357,7 +357,7 @@
357
357
],
358
358
"actions": [
359
359
"open('05-find.js')",
360
-
"set('const courses = require('./data/myCourses2');\n// Array.find(fn)\n\n// filter for the course title matching \"Web Security\"\nconst myClass = courses.filter(::>);\n')"
360
+
"set('import courses from './data/myCourses2';\n// Array.find(fn)\n\n// filter for the course title matching \"Web Security\"\nconst myClass = courses.filter(::>);\n')"
361
361
],
362
362
"hints": [
363
363
"create a `filter` function that takes a param `course`",
@@ -435,69 +435,15 @@
435
435
]
436
436
},
437
437
{
438
-
"description": "First, test out `flatten` on the `flattenedArray`",
438
+
"description": "First, test out `flatten` on the `flattenedArray`\nArray -> anything\n\nWe know our likely suspect is also in the school computer system. Perhaps our suspect also changed his grades.\n\nYou can't be sure who is a cheater, but you can assume if the grades are well above the average, the person is likely to be the culprit. For this, we'll have to do some basic statistical calculations. We'll need a new tool for transforming arrays into different data representations.\n\n`map` has a major limitation: it will always output the same number of elements as the input array.\n\nWhen you want to transform data into something different, you'll likely want to use `reduce`.\n\nReduce requires two parameters:\n\n * the running total (set by an initialValue)\n * the next value in the array\n\n```js\nfunction add(total, next) {\n console.log(`add(${total}, ${next}) -> ${total + next}`)\n return total + next\n}\n\nconst initialValue = 100;\n[1, 5, 10].reduce(add, initialValue); // initial value\n\n// add(100, 1) -> 101\n// add(101, 5) -> 106\n// add(106, 10) -> 116\n//> 116\n```\n\nNotice in the example we input an array of 3 items and output a single number. The data has been transformed.\n\nIt takes a while to wrap your head around `reduce`, but once you do, you'll see it's usefulness everywhere.\n\nYou may have noticed we've already used `reduce` to `flatten` our arrays.\n\n```js\nArray.prototype.flatten = function() {\n return this.reduce((a, b) => a.concat(b), []);\n};\n```\n\nWith `flatten`, the initialValue was set to an empty array which each value was `concat` onto.\n\nDo some practice with `reduce`, before you use it to narrow down a cheating suspect.",
439
439
"tests": [
440
440
"06/02"
441
441
],
442
442
"actions": [
443
443
"open('06-concat.js')",
444
-
"set('const courses = require('./data/courses2').default;\n// Array.concat(any)\n\n// Array.prototype can be used to create new Array methods\nArray.prototype.flatten = function() {\n return this.reduce((a, b) => a.concat(b), []);\n};\n')",
445
-
"insert('\nconst numberedList = [[1, 2], [3, 4]];\n\n// use `flatten` on `numberedList`\nconst flattenedArray = numberedList::>;\n')"
446
-
],
447
-
"hints": [
448
-
"call `.flatten()` on `numberedList`"
449
-
]
450
-
},
451
-
{
452
-
"description": "Now `map` over the courses array, and `map` over the students array inside of it.\nReturn the fields:\n\n * title\n * instructor\n * name\n * grade\n * score",
453
-
"tests": [
454
-
"06/03"
455
-
],
456
-
"actions": [
457
-
"insert('\n// map over courses then\n// map over students inside of courses\nconst doubleArray = courses.map((course) => {\n return course.students.map((student) => {\n return {\n // fill in the fields\n title: ::>'',\n instructor: '',\n name: '',\n score: '',\n grade: ''\n };\n });\n});\n\n')"
458
-
],
459
-
"hints": [
460
-
"pair `course.title`",
461
-
"pair `student.name`"
444
+
"set('```\nimport courses from './data/courses2');\n// Array.concat(any)\n\n// Array.prototype can be used to create new Array methods\nArray.prototype.flatten = function() {\n return this.reduce((a, b) => a.concat(b), []);\n};\n```\n))\n@action(insert(\n```\n\nconst numberedList = [[1, 2], [3, 4]];\n\n// use `flatten` on `numberedList`\nconst flattenedArray = numberedList::>;\n``` \n))\n@hint('call `.flatten()` on `numberedList`')\n\n\n+ Now `map` over the courses array, and `map` over the students array inside of it.\nReturn the fields:\n\n * title\n * instructor\n * name\n * grade\n * score\n@test('06/03')\n@action(insert(\n```\n\n// map over courses then\n// map over students inside of courses\nconst doubleArray = courses.map((course) => {\n return course.students.map((student) => {\n return {\n // fill in the fields\n title: ::>'',\n instructor: '',\n name: '',\n score: '',\n grade: ''\n };\n });\n});\n\n```\n))\n@hint('pair `course.title`')\n@hint('pair `student.name`')\n\n+ Use `flatten` to put all data into a single array. Set `students` to the result.\n@test('06/04')\n@action(insert(\n```\n// `flatten` doubleArray\nconst students = doubleArray::>;\n```\n))\n@hint('call `.flatten()` on `doubleArray`')\n\n+ Use the `suspects` array to `filter` to only data matching the names in the `suspects` array\n@test('06/05')\n@action(insert(\n```\n\nconst suspects = [\"Hack Kerr\"];\n// filter to data matching `suspects`\n\nconst suspectData = students::>;\n```\n))\n\n+ You just thought of two more suspects! Make a new variable called `newSuspects` and add it above `suspects`.\n\n```js\nconst newSuspects = ['Albert Gonzalez', 'Kevin Mitnick'];\n```\n\n`concat` the `newSuspects` onto the `suspects` list.\n@test('06/06')\n@hint('call `suspects.concat()` with `newSuspects`')\n\n\n## redu')"
462
445
]
463
446
},
464
-
{
465
-
"description": "Use `flatten` to put all data into a single array. Set `students` to the result.",
466
-
"tests": [
467
-
"06/04"
468
-
],
469
-
"actions": [
470
-
"insert('// `flatten` doubleArray\nconst students = doubleArray::>;\n')"
471
-
],
472
-
"hints": [
473
-
"call `.flatten()` on `doubleArray`"
474
-
]
475
-
},
476
-
{
477
-
"description": "Use the `suspects` array to `filter` to only data matching the names in the `suspects` array",
478
-
"tests": [
479
-
"06/05"
480
-
],
481
-
"actions": [
482
-
"insert('\nconst suspects = [\"Hack Kerr\"];\n// filter to data matching `suspects`\n\nconst suspectData = students::>;\n')"
483
-
]
484
-
},
485
-
{
486
-
"description": "You just thought of two more suspects! Make a new variable called `newSuspects` and add it above `suspects`.\n\n```js\nconst newSuspects = ['Albert Gonzalez', 'Kevin Mitnick'];\n```\n\n`concat` the `newSuspects` onto the `suspects` list.",
487
-
"tests": [
488
-
"06/06"
489
-
],
490
-
"hints": [
491
-
"call `suspects.concat()` with `newSuspects`"
492
-
]
493
-
}
494
-
],
495
-
"onPageComplete": "In the next step, we'll look at using one of the most powerful methods: `reduce`"
496
-
},
497
-
{
498
-
"title": "reduce",
499
-
"description": "Array -> anything\n\nWe know our likely suspect is also in the school computer system. Perhaps our suspect also changed his grades.\n\nYou can't be sure who is a cheater, but you can assume if the grades are well above the average, the person is likely to be the culprit. For this, we'll have to do some basic statistical calculations. We'll need a new tool for transforming arrays into different data representations.\n\n`map` has a major limitation: it will always output the same number of elements as the input array.\n\nWhen you want to transform data into something different, you'll likely want to use `reduce`.\n\nReduce requires two parameters:\n\n * the running total (set by an initialValue)\n * the next value in the array\n\n```js\nfunction add(total, next) {\n console.log(`add(${total}, ${next}) -> ${total + next}`)\n return total + next\n}\n\nconst initialValue = 100;\n[1, 5, 10].reduce(add, initialValue); // initial value\n\n// add(100, 1) -> 101\n// add(101, 5) -> 106\n// add(106, 10) -> 116\n//> 116\n```\n\nNotice in the example we input an array of 3 items and output a single number. The data has been transformed.\n\nIt takes a while to wrap your head around `reduce`, but once you do, you'll see it's usefulness everywhere.\n\nYou may have noticed we've already used `reduce` to `flatten` our arrays.\n\n```js\nArray.prototype.flatten = function() {\n return this.reduce((a, b) => a.concat(b), []);\n};\n```\n\nWith `flatten`, the initialValue was set to an empty array which each value was `concat` onto.\n\nDo some practice with `reduce`, before you use it to narrow down a cheating suspect.",
500
-
"tasks": [
501
447
{
502
448
"description": "load suspectData. We will come back to this after some practice;",
503
449
"tests": [
@@ -515,7 +461,7 @@
515
461
],
516
462
"actions": [
517
463
"open('07-reduce.js')",
518
-
"set('const courses = require('./data/courses2');\n// Array.reduce(fn(a, b), initialValue)\n\nconst practice = [1, 1, 2, 3, 5, 8, 13, 21];\n\nfunction add(a, b) {\n return a + b;\n}\n\n// total the numbers using a reduce function\nconst total = practice.reduce(::>);\n')"
464
+
"set('import courses from './data/courses2';\n// Array.reduce(fn(a, b), initialValue)\n\nconst practice = [1, 1, 2, 3, 5, 8, 13, 21];\n\nfunction add(a, b) {\n return a + b;\n}\n\n// total the numbers using a reduce function\nconst total = practice.reduce(::>);\n')"
519
465
],
520
466
"hints": [
521
467
"with only numbers, the initialValue defaults to 0",
@@ -587,7 +533,8 @@
587
533
"insert('console.log(likelySuspects);\n')"
588
534
]
589
535
}
590
-
]
536
+
],
537
+
"onPageComplete": "In the next step, we'll look at using one of the most powerful methods: `reduce`"
0 commit comments