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
@@ -32,6 +29,123 @@ CodeRoad is an open-sourced interactive tutorial platform for the Atom Editor. L
32
29
33
30
## Outline
34
31
32
+
##### Start
33
+
34
+
Understanding the Data Set
35
+
36
+
Over this tutorial series, we'll be changing and working with two different data sets. It'll be a big help to first understand what the data looks like.
37
+
38
+
```json
39
+
var students = [
40
+
{
41
+
"title": "Relational Databases",
42
+
"instructor": "Sean Quentin Lewis",
43
+
"name": "Ada Lovelace",
44
+
"score": 91,
45
+
"grade": "A"
46
+
},
47
+
...
48
+
]
49
+
```
50
+
51
+
Here we have an array of "student" objects. To get the first item in the array, you can use the array index. Array indexes start at 0.
52
+
53
+
```js
54
+
console.log(
55
+
'first instructor', students[0].instructor
56
+
);
57
+
// first instructor Sean Quentin Lewis
58
+
```
59
+
60
+
##### Filter
61
+
62
+
Array -> Array of items that match a condition
63
+
64
+
You've hacked into the school's computer system, and just in time. The grades are in, but you're not too proud of your performance. That's okay, you have a plan: you're going to create a fake report card.
65
+
66
+
It would be great if you could `filter` the scores that your parents will see.
67
+
68
+
`filter` takes a matching condition function and only returns items that result in true. As an example, look at `isA` below:
69
+
70
+
```
71
+
function isA(x) {
72
+
return x === 'a';
73
+
}
74
+
```
75
+
76
+
77
+
Like all of the methods in this chapter, `filter` is already part of the `Array.prototype`, so you can run it following any array. Each item in the array is passed into the params of the condition function, one by one. [Learn more](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter).
78
+
79
+
```
80
+
const list = ['a', 'b'];
81
+
list.filter(isA);
82
+
83
+
// if isA(list[0]), add to output array
84
+
// if isA(list[1]), add to output array
85
+
//
86
+
//> ['a']
87
+
```
88
+
89
+
If your data was composed of objects, we could use dot notation to find matches. Checkout `isB` below.
90
+
91
+
```
92
+
function isB(x) {
93
+
return x.item === 'b'
94
+
}
95
+
96
+
const list = [{item: 'a'}, {item: 'b'}];
97
+
list.filter(isB);
98
+
//> [{item: 'b'}]
99
+
```
100
+
101
+
Where were we? Back to filtering our grades.
102
+
103
+
There's too much student data in the computer system. We'll have to sort through it. Have a look at an example below:
104
+
105
+
```
106
+
console.log(students[0]);
107
+
//> { course: 'Web Security',
108
+
// instructor: 'Sue Denim',
109
+
// name: 'Rebecca Heineman',
110
+
// score: 93,
111
+
// grade: 'A' }
112
+
```
113
+
114
+
##### Sort
115
+
116
+
Array -> sorted Array
117
+
118
+
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.
119
+
120
+
You can use the array method `sort` to arrange your data. Let's see how it works.
121
+
122
+
```js
123
+
['c', 'b', 'a'].sort();
124
+
//> ['a', 'b', 'c']
125
+
126
+
[3, 2, 1].sort();
127
+
//> [1, 2, 3]
128
+
```
129
+
130
+
But what about sorting scores inside of an object?
131
+
132
+
```js
133
+
[{a:3}, {a:1}, {a:2}].sort();
134
+
//> [{a: 3}, {a: 1}, {a: 2}]
135
+
```
136
+
137
+
That didn't work. Instead, you can write a custom `compareScore` function.
138
+
139
+
A 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:
140
+
141
+
* -1 : sort to a lower index (front)
142
+
* 1 : sort to a higher index (back)
143
+
* 0 : stay the same
144
+
145
+
Alright, now time to sort your best grades to the top.
146
+
147
+
First you'll need to write a sort condition function called `compareScore`.
148
+
35
149
##### Map
36
150
37
151
Array -> run a function over each item -> Array
@@ -111,7 +225,7 @@ Know it or not, you're probably already used to "imperative" programming.
111
225
Imperative code tells the computer what to do, step by step.
112
226
113
227
```js
114
-
var x =1; // make a variable
228
+
let x =1; // make a variable
115
229
x = x +1; // add one
116
230
x = x +1; // add another
117
231
console.log(x);
@@ -140,7 +254,7 @@ A function is "pure" if it doesn't change anything outside of its scope. Pure fu
140
254
On the other hand, **impure** functions are less predictable. The result may be different if you call it at a later time.
141
255
142
256
```js
143
-
var y =1;
257
+
let y =1;
144
258
// impure function
145
259
functionincrement(x) {
146
260
y += x;
@@ -191,7 +305,7 @@ You quickly put together a list of other students in class. If someone changed y
191
305
`find` works similar to `filter`, but returns only the first match.
192
306
193
307
```
194
-
var data = [1, 2, 3, 4, 5, 6];
308
+
const data = [1, 2, 3, 4, 5, 6];
195
309
196
310
function isEven(num) {
197
311
return num % 2 === 0;
@@ -274,7 +388,7 @@ Unfortunately, Javascript is missing a built in array method to concat multiple
274
388
Let's look at an abstraction of what we need to do:
275
389
276
390
```js
277
-
var start = [{
391
+
conststart= [{
278
392
a:1,
279
393
c: [
280
394
{ b:1 }
@@ -286,7 +400,7 @@ var start = [{
286
400
]
287
401
}];
288
402
289
-
var middle =start.map(function(outer) {
403
+
constmiddle=start.map(function(outer) {
290
404
returnouter.c.map(function(inner) {
291
405
return {
292
406
a:outer.a,
@@ -296,7 +410,7 @@ var middle = start.map(function(outer) {
@hint('set `course.score` to `Math.min(95, course.score + 12)`')
188
188
189
-
+ One more problem. Now the scores don't match the grades. you have 95 score in "3D Computer Graphics", but only a "B" grade. Set `myFixed` as the result of using the `getGrade` function to set grades according to their new scores.
189
+
+ One more problem. Now the scores don't match the grades. you have 95 score in "3D Computer Graphics", but only a "B" grade. Update your `increaseScore` function to also update the grade by using the `getGrade` function
190
190
@test('03/06')
191
191
@action(insert(
192
192
```
193
193
194
-
// change `getGrade` to accept a "course" instead of a "score"
195
-
// and return that updated "course"
194
+
// use getGrade to set the course grade
195
+
// update `increaseScore` to also update the grade
196
196
function getGrade(score) {
197
197
switch (true) {
198
198
case (score >= 90):
@@ -208,13 +208,10 @@ function getGrade(score) {
208
208
}
209
209
}
210
210
211
-
// map `myFixed` to update grades to the new scores
212
-
const myFixed = mySlightlyChanged;
213
211
```
214
212
))
215
-
@hint('change `getGrade` to take a `course` param instead of `score`')
216
-
@hint('change the grade and return the `course`')
217
-
@hint('set `course.grade = "A"` and return `course`')
213
+
@hint('call `getGrade` inside of `increaseScore`')
214
+
@hint('the `increaseScore` function should set course.grade equal to `getGrade(course.score)`')
218
215
219
216
+ Check to make sure everything is working. Set `scoresAndGrades` to an array of scores and grades only.
0 commit comments