Skip to content

Commit 47924c7

Browse files
committed
demo filter, sort complete
1 parent 0a54f91 commit 47924c7

File tree

13 files changed

+158
-130
lines changed

13 files changed

+158
-130
lines changed

coderoad.json

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"1/01/02-filter"
3030
],
3131
"actions": [
32-
"insert('// call filter condition here\nvar myData = data.filter();\n\n')"
32+
"insert('// call filter condition here\nvar myData = data.filter();\n')"
3333
]
3434
},
3535
{
@@ -38,7 +38,7 @@
3838
"1/01/03-filter"
3939
],
4040
"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')"
4242
]
4343
},
4444
{
@@ -55,22 +55,34 @@
5555
{
5656
"title": "Sort",
5757
"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`.",
5959
"tasks": [
6060
{
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",
6262
"tests": [
6363
"1/02/01-sort"
6464
],
6565
"actions": [
6666
"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')"
6868
]
6969
},
7070
{
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",
7272
"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"
7486
],
7587
"actions": [
7688
"insert('// use the compare function to sort myBest\nvar mySorted = myBest\n')"
@@ -81,20 +93,20 @@
8193
{
8294
"title": "Map",
8395
"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.",
8597
"tasks": [
8698
{
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.",
88100
"tests": [
89101
"1/03/01-map"
90102
],
91103
"actions": [
92104
"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')"
94106
]
95107
},
96108
{
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.",
98110
"tests": [
99111
"1/03/02-map"
100112
],
@@ -103,7 +115,7 @@
103115
]
104116
},
105117
{
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.",
107119
"tests": [
108120
"1/03/03-map"
109121
],
@@ -112,7 +124,7 @@
112124
]
113125
},
114126
{
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.",
116128
"tests": [
117129
"1/03/04-map"
118130
],
@@ -126,7 +138,16 @@
126138
"1/03/05-map"
127139
],
128140
"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')"
130151
]
131152
}
132153
]

tutorial/1/01/filter.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,16 @@ function isAda() {
8787
```
8888
// call filter condition here
8989
var myData = data.filter();
90-
9190
```
9291
))
9392

9493
+ Write a filter condition called `isGoodGrade` that will filter out any "D" or "F" grades.
9594
@test('1/01/03-filter')
9695
@action(insert(
9796
```
98-
// return true if student "grade" is not a "D" or "F"
97+
98+
99+
// return true if student.grade is not a "D" or "F"
99100
function isGoodGrade(student) {
100101
101102
}

tutorial/1/02/01-sort.spec.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,7 @@ describe('01 function compareScore', function () {
1818
expect(compareScore.length).to.equal(2);
1919
});
2020

21-
it('doesn\'t return 1 when b\'s score is less than a\'s', function() {
22-
expect(compareScore(5, 3)).to.equal(1);
21+
it('doesn\'t return 1 when b\'s score is more than a\'s', function() {
22+
expect(compareScore({score: 3}, {score: 5})).to.equal(1);
2323
});
24-
25-
it('doesn\'t return -1 when b\'s score is more than a\'s', function() {
26-
expect(compareScore(3, 5)).to.equal(-1);
27-
});
28-
29-
it('doesn\'t return 0 when b\'s score equals a\'s', function() {
30-
expect(compareScore(3, 3)).to.equal(0);
31-
});
32-
3324
});

tutorial/1/02/02-sort.spec.js

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,5 @@
1-
'use strict';
2-
var expect = require('chai').expect;
3-
4-
describe('02 var mySorted', function() {
5-
6-
it('doesn\'t exist', function() {
7-
expect(mySorted).to.not.be.undefined;
1+
describe('02 function compareScore', function () {
2+
it('doesn\'t return -1 when b\'s score is less than a\'s', function() {
3+
expect(compareScore({score: 5}, {score: 3})).to.equal(-1);
84
});
9-
10-
it('doesn\'t output an array', function() {
11-
expect(mySorted).to.be.an('array');
12-
});
13-
14-
it('doesn\'t output exactly seven items', function() {
15-
expect(mySorted).to.have.length(7);
16-
});
17-
18-
it('isn\'t the right sorted data', function() {
19-
function compareScore(a, b) {
20-
switch (true) {
21-
case b.score < a.score:
22-
return 1;
23-
case b.score > a.score:
24-
return -1;
25-
default:
26-
return 0;
27-
}
28-
}
29-
expect(mySorted).to.deep.equal([{
30-
title: 'Relational Databases',
31-
instructor: 'Sean Quentin Lewis',
32-
name: 'Ada Lovelace',
33-
score: 91,
34-
grade: 'A'
35-
}, {
36-
title: '3D Computer Graphics',
37-
instructor: 'G.L. Webb',
38-
name: 'Ada Lovelace',
39-
score: 88,
40-
grade: 'B'
41-
}, {
42-
title: 'Web Security',
43-
instructor: 'Sue Denim',
44-
name: 'Ada Lovelace',
45-
score: 81,
46-
grade: 'B'
47-
}, {
48-
title: 'Javascript Fundamentals',
49-
instructor: 'Jay Kweerie',
50-
name: 'Ada Lovelace',
51-
score: 73,
52-
grade: 'C'
53-
}, {
54-
title: 'Algorithm Design',
55-
instructor: 'Gale Shapely',
56-
name: 'Ada Lovelace',
57-
score: 93,
58-
grade: 'A'
59-
}, {
60-
title: 'Data Abstraction',
61-
instructor: 'Aster Ricks',
62-
name: 'Ada Lovelace',
63-
score: 82,
64-
grade: 'B'
65-
}, {
66-
title: 'Data Structures',
67-
instructor: 'Brodal Q.',
68-
name: 'Ada Lovelace',
69-
score: 88,
70-
grade: 'B'
71-
}]);
72-
});
73-
745
});

tutorial/1/02/03-sort.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('03 function compareScore', function() {
2+
it('doesn\'t return 0 when b\'s score equals a\'s', function() {
3+
expect(compareScore({score: 3}, {score: 3})).to.equal(0);
4+
});
5+
});

tutorial/1/02/04-sort.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
var expect = require('chai').expect;
3+
4+
describe('04 var mySorted', function() {
5+
6+
it('doesn\'t exist', function() {
7+
expect(mySorted).to.not.be.undefined;
8+
});
9+
10+
it('doesn\'t output an array', function() {
11+
expect(mySorted).to.be.an('array');
12+
});
13+
14+
it('doesn\'t output exactly seven items', function() {
15+
expect(mySorted).to.have.length(7);
16+
});
17+
18+
it('isn\'t the right sorted data', function() {
19+
expect(mySorted[0].score).to.equal(93);
20+
expect(mySorted[6].score).to.equal(73);
21+
});
22+
23+
});

tutorial/1/02/sort.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,36 @@ A sort function takes two params, and compares the first to the second. It shoul
3030

3131
Alright, now time to sort your best grades to the top.
3232

33-
+ Write a sort condition function called `compareScore` that can sort your data by score.
33+
First you'll need to write a sort condition function called `compareScore`.
34+
35+
+ `compareScore` should return 1 if the first score is less than the second
3436
@test('1/02/01-sort')
3537
@action(open('02-sort.js'))
3638
@action(set(
3739
```
3840
function compareScore(a, b) {
3941
switch (true) {
40-
case b < a:
41-
// it should return 1 if b's score is less than a's
42-
return;
43-
case b > a:
44-
// it should return -1 if b's score is more than a's
45-
return;
42+
case b.score > a.score:
43+
// it should return 1 if b's score is more than a's
44+
return
45+
case 'set condition here':
46+
// it should return -1 if b's score is less than a's
47+
4648
default:
47-
// it should return 0 if b has the same score as a
48-
return;
49+
// it should return 0 if b and a have the same score
50+
4951
}
5052
}
5153
```
5254
))
55+
+ `compareScore` should return -1 if the first score is more than the second
56+
@test('1/02/02-sort')
5357

54-
+ Set `mySortedGrades` to `myBest` data sorted by `compareScore`
55-
@test('1/02/01-sort')
58+
+ `compareScore` should return 0 if the first score is the same as the second
59+
@test('1/02/03-sort')
60+
61+
+ Set `mySorted` to the result of `myBest` sorted by `compareScore`
62+
@test('1/02/04-sort')
5663
@action(insert(
5764
```
5865
// use the compare function to sort myBest

tutorial/1/03/01-map.spec.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,27 @@ describe('01 function changeGrades', function() {
1515
expect(changeGrades).to.be.a('function');
1616
});
1717

18-
it('shouldn\'t change other grades to an A', function() {
18+
it('should change grades from a D to an A', function() {
1919
var test = {
20-
grade: 'B'
20+
grade: 'D'
2121
};
22-
expect(changeGrades(test)).to.deep.equal(test);
23-
})
22+
expect(arrayOfGrades).to.deep.equal({
23+
grade: 'A'
24+
});
25+
});
2426

25-
it('doesn\'t change grades from a D to an A', function() {
27+
it('should change grades from a F to an A', function() {
2628
var test = {
27-
grade: 'D'
29+
grade: 'F'
2830
};
2931
expect(arrayOfGrades).to.deep.equal({
3032
grade: 'A'
3133
});
3234
});
3335

34-
it('doesn\'t change grades from a F to an A', function() {
36+
it('should change grades from a B to an A', function() {
3537
var test = {
36-
grade: 'F'
38+
grade: 'B'
3739
};
3840
expect(arrayOfGrades).to.deep.equal({
3941
grade: 'A'

tutorial/1/03/06-map.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var expect = require('chai').expect;
2+
3+
describe('06 var scoresAndGrades', function() {
4+
5+
it('should return an array of scores and grades', function() {
6+
expect(scoresAndGrades[0]).to.deep.equal([{
7+
grade: "A",
8+
score: 95
9+
}]);
10+
expect(scoresAndGrades[9]).to.deep.equal([{
11+
grade: "C",
12+
score: 77
13+
}]);
14+
});
15+
});

0 commit comments

Comments
 (0)