|
29 | 29 | "1/01/02-filter"
|
30 | 30 | ],
|
31 | 31 | "actions": [
|
32 |
| - "insert('// call filter condition here\nvar myData = data.filter();\n\n')" |
| 32 | + "insert('// call filter condition here\nvar myData = data.filter();\n')" |
33 | 33 | ]
|
34 | 34 | },
|
35 | 35 | {
|
|
38 | 38 | "1/01/03-filter"
|
39 | 39 | ],
|
40 | 40 | "actions": [
|
41 |
| - "insert('// return true if student \"grade\" is not a \"D\" or \"F\"\nfunction isGoodGrade(student) {\n\n}\n')" |
| 41 | + "insert('\n\n// return true if student.grade is not a \"D\" or \"F\"\nfunction isGoodGrade(student) {\n\n}\n')" |
42 | 42 | ]
|
43 | 43 | },
|
44 | 44 | {
|
|
55 | 55 | {
|
56 | 56 | "title": "Sort",
|
57 | 57 | "description": "Array -> sorted Array",
|
58 |
| - "explanation": "Your grades are filtered down to your name and good scores - but wouldn't it be better if your best grades were displayed first, at the top? Besides, your parents rarely read anything through.\n\nYou can use the array method `sort` to arrange your data. Let's see how it works.\n\n```js\n['c', 'b', 'a'].sort();\n//> ['a', 'b', 'c']\n\n[3, 2, 1].sort();\n//> [1, 2, 3]\n```\n\nBut what about sorting scores inside of an object?\n\n```js\n[{a: 3}, {a: 1}, {a: 2}].sort();\n//> [{a: 3}, {a: 1}, {a: 2}]\n```\n\nThat didn't work. Instead, you can write a custom `compareScore` function.\n\nA sort function takes two params, and compares the first to the second. It should return values saying where the second value should go in the array:\n\n * -1 : sort to a lower index (front)\n * 1 : sort to a higher index (back)\n * 0 : stay the same\n\nAlright, now time to sort your best grades to the top.", |
| 58 | + "explanation": "Your grades are filtered down to your name and good scores - but wouldn't it be better if your best grades were displayed first, at the top? Besides, your parents rarely read anything through.\n\nYou can use the array method `sort` to arrange your data. Let's see how it works.\n\n```js\n['c', 'b', 'a'].sort();\n//> ['a', 'b', 'c']\n\n[3, 2, 1].sort();\n//> [1, 2, 3]\n```\n\nBut what about sorting scores inside of an object?\n\n```js\n[{a: 3}, {a: 1}, {a: 2}].sort();\n//> [{a: 3}, {a: 1}, {a: 2}]\n```\n\nThat didn't work. Instead, you can write a custom `compareScore` function.\n\nA sort function takes two params, and compares the first to the second. It should return values saying where the second value should go in the array:\n\n * -1 : sort to a lower index (front)\n * 1 : sort to a higher index (back)\n * 0 : stay the same\n\nAlright, now time to sort your best grades to the top.\n\nFirst you'll need to write a sort condition function called `compareScore`.", |
59 | 59 | "tasks": [
|
60 | 60 | {
|
61 |
| - "description": "Write a sort condition function called `compareScore` that can sort your data by score.", |
| 61 | + "description": "`compareScore` should return 1 if the first score is less than the second", |
62 | 62 | "tests": [
|
63 | 63 | "1/02/01-sort"
|
64 | 64 | ],
|
65 | 65 | "actions": [
|
66 | 66 | "open('02-sort.js')",
|
67 |
| - "set('function compareScore(a, b) {\n switch (true) {\n case b < a:\n // it should return 1 if b's score is less than a's\n return;\n case b > a:\n // it should return -1 if b's score is more than a's\n return;\n default:\n // it should return 0 if b has the same score as a\n return;\n }\n}\n')" |
| 67 | + "set('function 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')" |
68 | 68 | ]
|
69 | 69 | },
|
70 | 70 | {
|
71 |
| - "description": "Set `mySortedGrades` to `myBest` data sorted by `compareScore`", |
| 71 | + "description": "`compareScore` should return -1 if the first score is more than the second", |
72 | 72 | "tests": [
|
73 |
| - "1/02/01-sort" |
| 73 | + "1/02/02-sort" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "description": "`compareScore` should return 0 if the first score is the same as the second", |
| 78 | + "tests": [ |
| 79 | + "1/02/03-sort" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "description": "Set `mySorted` to the result of `myBest` sorted by `compareScore`", |
| 84 | + "tests": [ |
| 85 | + "1/02/04-sort" |
74 | 86 | ],
|
75 | 87 | "actions": [
|
76 | 88 | "insert('// use the compare function to sort myBest\nvar mySorted = myBest\n')"
|
|
81 | 93 | {
|
82 | 94 | "title": "Map",
|
83 | 95 | "description": "Array -> run a function over each item -> Array",
|
84 |
| - "explanation": "You've filtered and sorted our data, but neither of those actually change the data.\n\nWouldn't it be simpler if you could just change your grades? You can use the array method `map` to run a function that returns changes to your data.\n\nAs an example, let's look at how you would increment each number in an array.\n\n```\nfunction addOne(num) {\n return num + 1;\n}\n\nfunction addTwo(num) {\n return num + 2;\n}\n\n[1, 2, 3].map(addOne);\n//> [2, 3, 4]\n\n[1, 2, 3].map(addOne).map(addTwo);\n//> [4, 5, 6]\n```\n\n`map` is powerful.\n\nThose D & F grades would look a lot better if they suddenly became A's.\n\nLet's go back to before we filtered out the bad grades, and instead change them.", |
| 96 | + "explanation": "You've filtered and sorted our data, but neither of those actually change the data.\n\nWouldn't it be simpler if you could just change your grades? You can use the array method `map` to run a function that returns changes to your data.\n\nAs an example, let's look at how you would increment each number in an array.\n\n```js\nfunction addOne(num) {\n return num + 1;\n}\n\nfunction addTwo(num) {\n return num + 2;\n}\n\n[1, 2, 3].map(addOne);\n//> [2, 3, 4]\n\n[1, 2, 3].map(addOne).map(addTwo);\n//> [4, 5, 6]\n```\n\n`map` can change data, but it can also restrict the data you want to work with. See the example below to see another way scores could be sorted.\n\n```js\nmyBest\n .map(function(student) {\n return student.score;\n })\n .sort()\n .reverse()\n//> [93, 91, 88, 88, 82, 81, 73]\n```\n\nIn this example, `map` transformed an object with keys of 'title', 'instructor', 'name', 'score' and 'grade', to an array of scores only. Data wasn't changed, but it was limited to a smaller subset of scores.\n\n`map` is powerful. Let's see what you can do with it.\n\nThose D & F grades would look a lot better if they suddenly became A's.\n\nLet's go back to before we filtered out the bad grades, and instead change the grades to A's.", |
85 | 97 | "tasks": [
|
86 | 98 | {
|
87 |
| - "description": "Make a function `changeGrades` that takes student data and changes any \"D\"s and \"F\"s to \"A\"s.", |
| 99 | + "description": "Make a function `changeGrades` that takes student data and changes all grades to \"A\"s.", |
88 | 100 | "tests": [
|
89 | 101 | "1/03/01-map"
|
90 | 102 | ],
|
91 | 103 | "actions": [
|
92 | 104 | "open('03-map.js')",
|
93 |
| - "set('// change any `student.grade`'s that are \"D\"'s or \"F\"'s into A's\nfunction changeGrades(student) {\n\n}\n')" |
| 105 | + "set('// change any `student.grade`'s into an 'A'\nfunction changeGrades(student) {\n\n}\n')" |
94 | 106 | ]
|
95 | 107 | },
|
96 | 108 | {
|
97 |
| - "description": "Map over the student data with the `changeGrades` function", |
| 109 | + "description": "Map over the `myData` with the `changeGrades` function. Set `myChanged` to the result.", |
98 | 110 | "tests": [
|
99 | 111 | "1/03/02-map"
|
100 | 112 | ],
|
|
103 | 115 | ]
|
104 | 116 | },
|
105 | 117 | {
|
106 |
| - "description": "Hold up. An A in \"Data Science\" class looks way to suspicious. Your parents might catch on to your cheating. Instead, let's go back to myData and increment each score by 12 points.", |
| 118 | + "description": "Hold up. An A in \"Data Science\" class looks way to suspicious. Your parents might catch on to your cheating.\n\nLet's go back to `myData` and instead increment each score by 12 points.", |
107 | 119 | "tests": [
|
108 | 120 | "1/03/03-map"
|
109 | 121 | ],
|
|
112 | 124 | ]
|
113 | 125 | },
|
114 | 126 | {
|
115 |
| - "description": "Wait. Now you're getting 105 in \"Algorithm Design\" class. Set `mySlightlyFixed` to your scores with a maximum score of 95. That should be less suspicious.", |
| 127 | + "description": "Wait. Now you're getting 105 in \"Algorithm Design\" class. Set `mySlightlyFixed` to have a maximum score of 95. That should be less suspicious.", |
116 | 128 | "tests": [
|
117 | 129 | "1/03/04-map"
|
118 | 130 | ],
|
|
126 | 138 | "1/03/05-map"
|
127 | 139 | ],
|
128 | 140 | "actions": [
|
129 |
| - "insert('function getGrade(score) {\n switch (true) {\n case (score >= 90):\n return \"A\";\n case (score >= 80):\n return \"B\";\n case (score >= 70):\n return \"C\";\n case (score >= 60):\n return \"D\";\n default:\n return \"F\";\n }\n}\n// set `myFixed` to update grades to the new scores\nvar myFixed = mySlightlyChanged.map();\n')" |
| 141 | + "insert('\nfunction getGrade(score) {\n switch (true) {\n case (score >= 90):\n return \"A\";\n case (score >= 80):\n return \"B\";\n case (score >= 70):\n return \"C\";\n case (score >= 60):\n return \"D\";\n default:\n return \"F\";\n }\n}\n// set `myFixed` to update grades to the new scores\nvar myFixed = mySlightlyChanged.map();\n')" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "description": "Check to make sure everything is working. Set `scoresAndGrades` to an array of scores and grades only.", |
| 146 | + "tests": [ |
| 147 | + "1/03/06-map" |
| 148 | + ], |
| 149 | + "actions": [ |
| 150 | + "insert('\n// set `scoresAndGrades` to an array of scores and grades\n// it should return an array of objects like this: {score: 75, grade: 'C'}\nvar scoresAndGrades = myFixed;\n')" |
130 | 151 | ]
|
131 | 152 | }
|
132 | 153 | ]
|
|
0 commit comments