Skip to content

Commit 4c409a3

Browse files
committed
step 3 progress
1 parent 47924c7 commit 4c409a3

File tree

6 files changed

+141
-84
lines changed

6 files changed

+141
-84
lines changed

coderoad.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
{
9494
"title": "Map",
9595
"description": "Array -> run a function over each item -> Array",
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.",
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?\n\nYou 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\n[1, 2, 3].map(addOne);\n//> [2, 3, 4]\n\nfunction addToVal(obj) {\n obj.val += 1;\n return obj;\n}\n[{ val: 1}].map(addToVal);\n//> [{ val: 2 }]\n```\n\n`map` can change data, and it can also alter structure of the data you're working with.\n\n```js\nfunction makeObject(num) {\n return { val: num };\n}\n\n[1, 2].map(makeObject);\n//> [{ val: 1 }, { val: 2 }]\n```\n\nSimilarly, `map` 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 just scores. Values weren't changed, but rather 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.",
9797
"tasks": [
9898
{
9999
"description": "Make a function `changeGrades` that takes student data and changes all grades to \"A\"s.",
@@ -102,7 +102,7 @@
102102
],
103103
"actions": [
104104
"open('03-map.js')",
105-
"set('// change any `student.grade`'s into an 'A'\nfunction changeGrades(student) {\n\n}\n')"
105+
"set('// change any `student.grade`'s into an 'A'\nfunction changeGrades() {\n\n}\n')"
106106
]
107107
},
108108
{
@@ -120,7 +120,7 @@
120120
"1/03/03-map"
121121
],
122122
"actions": [
123-
"insert('// map over `mySlightlyChanged` with a function `increaseGrades` to increment each score by 12\nvar mySlightlyChanged = myData.map();\n')"
123+
"insert('\nfunction increaseScores() {\n\n}\n\n// map over `mySlightlyChanged` with a function `increaseScores` to increment each score by 12\nvar mySlightlyChanged = myData;\n')"
124124
]
125125
},
126126
{
@@ -129,7 +129,7 @@
129129
"1/03/04-map"
130130
],
131131
"actions": [
132-
"insert('// set `mySlightlyFixed` to change any scores over 100 to a score of 95\nvar mySlightlyFixed = mySlightlyChanged.map();\n')"
132+
"insert('\nfunction capScores() {\n\n}\n\n// set `mySlightlyFixed` to change any scores over 100 to a score of 95\nvar mySlightlyFixed = mySlightlyChanged;\n')"
133133
]
134134
},
135135
{
@@ -138,7 +138,7 @@
138138
"1/03/05-map"
139139
],
140140
"actions": [
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')"
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\n// set `myFixed` to update grades to the new scores\nvar myFixed = mySlightlyChanged;\n')"
142142
]
143143
},
144144
{

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

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

18+
it('should take a parameter', function() {
19+
expect(changeGrades).to.have.length(1);
20+
});
21+
22+
it('should try changing `student.grade` first before returning `student`', function () {
23+
var regex = /return [a-zA-Z]+\.grade/;
24+
var func = changeGrades.toString();
25+
expect(func.match(regex)).to.be.null;
26+
});
27+
1828
it('should change grades from a D to an A', function() {
1929
var test = {
2030
grade: 'D'
2131
};
22-
expect(arrayOfGrades).to.deep.equal({
32+
expect(changeGrades(test)).to.deep.equal({
2333
grade: 'A'
2434
});
2535
});
@@ -28,7 +38,7 @@ describe('01 function changeGrades', function() {
2838
var test = {
2939
grade: 'F'
3040
};
31-
expect(arrayOfGrades).to.deep.equal({
41+
expect(changeGrades(test)).to.deep.equal({
3242
grade: 'A'
3343
});
3444
});
@@ -37,7 +47,7 @@ describe('01 function changeGrades', function() {
3747
var test = {
3848
grade: 'B'
3949
};
40-
expect(arrayOfGrades).to.deep.equal({
50+
expect(changeGrades(test)).to.deep.equal({
4151
grade: 'A'
4252
});
4353
});

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

Lines changed: 36 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
var expect = require('chai').expect;
22

3+
describe('03 function increaseScores', function() {
4+
5+
it('doesn\'t exist', function() {
6+
expect(increaseScores).to.not.be.undefined;
7+
});
8+
9+
it('should be a function', function() {
10+
expect(increaseScores).to.be.a('function');
11+
});
12+
13+
it('should take a parameter', function() {
14+
expect(increaseScores).to.have.length(1);
15+
});
16+
17+
it('should try changing the `score` first before returning the changed object', function() {
18+
var regex = /return [a-zA-Z]+\.score/;
19+
var func = increaseScores.toString();
20+
expect(func.match(regex)).to.be.null;
21+
});
22+
23+
it('should increment scores by 12 points', function() {
24+
var test = {
25+
score: 50
26+
};
27+
expect(increaseScores(test)).to.deep.equal({
28+
score: 62
29+
});
30+
});
31+
32+
});
33+
334
describe('03 var mySlightlyChanged', function() {
435

536
it('doesn\'t exist', function() {
@@ -10,68 +41,11 @@ describe('03 var mySlightlyChanged', function() {
1041
expect(mySlightlyChanged).to.be.an('array');
1142
});
1243

13-
it('doesn\'t increment scores by 12', function() {
14-
expect(mySlightlyChanged).to.deep.equal([{
15-
"title": "Relational Databases",
16-
"instructor": "Sean Quentin Lewis",
17-
"name": "Ada Lovelace",
18-
"score": 103,
19-
"grade": "A"
20-
}, {
21-
"title": "3D Computer Graphics",
22-
"instructor": "G.L. Webb",
23-
"name": "Ada Lovelace",
24-
"score": 100,
25-
"grade": "B"
26-
}, {
27-
"title": "Front End Web Development",
28-
"instructor": "Moe Zaick",
29-
"name": "Ada Lovelace",
30-
"score": 73,
31-
"grade": "D"
32-
}, {
33-
"title": "Web Security",
34-
"instructor": "Sue Denim",
35-
"name": "Ada Lovelace",
36-
"score": 93,
37-
"grade": "B"
38-
}, {
39-
"title": "Javascript Fundamentals",
40-
"instructor": "Jay Kweerie",
41-
"name": "Ada Lovelace",
42-
"score": 85,
43-
"grade": "C"
44-
}, {
45-
"title": "Data Science",
46-
"instructor": "Ford Fulkerson",
47-
"name": "Ada Lovelace",
48-
"score": 70,
49-
"grade": "F"
50-
}, {
51-
"title": "Algorithm Design",
52-
"instructor": "Gale Shapely",
53-
"name": "Ada Lovelace",
54-
"score": 105,
55-
"grade": "A"
56-
}, {
57-
"title": "Data Abstraction",
58-
"instructor": "Aster Ricks",
59-
"name": "Ada Lovelace",
60-
"score": 94,
61-
"grade": "B"
62-
}, {
63-
"title": "Data Structures",
64-
"instructor": "Brodal Q.",
65-
"name": "Ada Lovelace",
66-
"score": 100,
67-
"grade": "B"
68-
}, {
69-
"title": "Networks",
70-
"instructor": "Van Emde Boas",
71-
"name": "Ada Lovelace",
72-
"score": 77,
73-
"grade": "D"
74-
}]);
44+
it('should increment scores by 12', function() {
45+
expect(mySlightlyChanged
46+
.map(function(x) {
47+
return x.score;
48+
})).to.deep.equal([103, 100, 73, 93, 85, 70, 105, 94, 100, 77]);
7549
});
7650

7751
});

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
var expect = require('chai').expect;
22

3+
describe('04 function capScores', function() {
4+
5+
it('doesn\'t exist', function() {
6+
expect(capScores).to.not.be.undefined;
7+
});
8+
9+
it('should be a function', function() {
10+
expect(capScores).to.be.a('function');
11+
});
12+
13+
it('should take a parameter', function() {
14+
expect(capScores).to.have.length(1);
15+
});
16+
17+
it('shouldn\'t change scores under 100', function() {
18+
var test = {
19+
score: 99
20+
};
21+
expect(capScores(test)).to.deep.equal({
22+
score: 99
23+
});
24+
});
25+
26+
it('should change scores over 100 to 95', function() {
27+
var test = {
28+
score: 101
29+
};
30+
expect(capScores(test)).to.deep.equal({
31+
score: 95
32+
});
33+
});
34+
35+
});
36+
337
describe('04 var mySlightlyFixed', function() {
438

539
it('doesn\'t exist', function() {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
var expect = require('chai').expect;
22

3+
describe('05 function getGrade', function() {
4+
5+
it('doesn\'t exist', function() {
6+
expect(getGrade).to.not.be.undefined;
7+
});
8+
9+
it('should be a function', function() {
10+
expect(getGrade).to.be.a('function');
11+
});
12+
13+
it('should take a parameter', function() {
14+
expect(getGrade).to.have.length(1);
15+
});
16+
});
17+
318
describe('05 var myFixed', function() {
419

520
it('doesn\'t exist', function() {

tutorial/1/03/map.md

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ Array -> run a function over each item -> Array
33

44
You've filtered and sorted our data, but neither of those actually change the data.
55

6-
Wouldn'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.
6+
Wouldn't it be simpler if you could just change your grades?
7+
8+
You can use the array method `map` to run a function that returns changes to your data.
79

810
As an example, let's look at how you would increment each number in an array.
911

@@ -12,18 +14,29 @@ function addOne(num) {
1214
return num + 1;
1315
}
1416

15-
function addTwo(num) {
16-
return num + 2;
17-
}
18-
1917
[1, 2, 3].map(addOne);
2018
//> [2, 3, 4]
2119

22-
[1, 2, 3].map(addOne).map(addTwo);
23-
//> [4, 5, 6]
20+
function addToVal(obj) {
21+
obj.val += 1;
22+
return obj;
23+
}
24+
[{ val: 1}].map(addToVal);
25+
//> [{ val: 2 }]
26+
```
27+
28+
`map` can change data, and it can also alter structure of the data you're working with.
29+
30+
```js
31+
function makeObject(num) {
32+
return { val: num };
33+
}
34+
35+
[1, 2].map(makeObject);
36+
//> [{ val: 1 }, { val: 2 }]
2437
```
2538

26-
`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.
39+
Similarly, `map` can also restrict the data you want to work with. See the example below to see another way scores could be sorted.
2740

2841
```js
2942
myBest
@@ -35,7 +48,7 @@ myBest
3548
//> [93, 91, 88, 88, 82, 81, 73]
3649
```
3750

38-
In 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.
51+
In this example, `map` transformed an object with keys of 'title', 'instructor', 'name', 'score' and 'grade', to an array of just scores. Values weren't changed, but rather limited to a smaller subset of scores.
3952

4053
`map` is powerful. Let's see what you can do with it.
4154

@@ -49,7 +62,7 @@ Let's go back to before we filtered out the bad grades, and instead change the g
4962
@action(set(
5063
```
5164
// change any `student.grade`'s into an 'A'
52-
function changeGrades(student) {
65+
function changeGrades() {
5366
5467
}
5568
```
@@ -71,17 +84,27 @@ Let's go back to `myData` and instead increment each score by 12 points.
7184
@test('1/03/03-map')
7285
@action(insert(
7386
```
74-
// map over `mySlightlyChanged` with a function `increaseGrades` to increment each score by 12
75-
var mySlightlyChanged = myData.map();
87+
88+
function increaseScores() {
89+
90+
}
91+
92+
// map over `mySlightlyChanged` with a function `increaseScores` to increment each score by 12
93+
var mySlightlyChanged = myData;
7694
```
7795
))
7896

7997
+ Wait. Now you're getting 105 in "Algorithm Design" class. Set `mySlightlyFixed` to have a maximum score of 95. That should be less suspicious.
8098
@test('1/03/04-map')
8199
@action(insert(
82100
```
101+
102+
function capScores() {
103+
104+
}
105+
83106
// set `mySlightlyFixed` to change any scores over 100 to a score of 95
84-
var mySlightlyFixed = mySlightlyChanged.map();
107+
var mySlightlyFixed = mySlightlyChanged;
85108
```
86109
))
87110

@@ -104,8 +127,9 @@ function getGrade(score) {
104127
return "F";
105128
}
106129
}
130+
107131
// set `myFixed` to update grades to the new scores
108-
var myFixed = mySlightlyChanged.map();
132+
var myFixed = mySlightlyChanged;
109133
```
110134
))
111135

0 commit comments

Comments
 (0)