Skip to content

Commit 4add7cf

Browse files
committed
03-map complete
1 parent 4c409a3 commit 4add7cf

File tree

10 files changed

+61
-194
lines changed

10 files changed

+61
-194
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ Array -> run a function for each item
4646

4747
##### find
4848

49-
Array -> find an item that matches a condition
49+
Array -> first element that matches a condition
5050

5151
##### reduce
5252

53-
Array -> transform into anything
53+
Array -> anything

coderoad.json

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
],
103103
"actions": [
104104
"open('03-map.js')",
105-
"set('// change any `student.grade`'s into an 'A'\nfunction changeGrades() {\n\n}\n')"
105+
"set('// change any `student.grade`'s into an 'A'\nfunction changeGrade() {\n\n}\n')"
106106
]
107107
},
108108
{
@@ -120,16 +120,13 @@
120120
"1/03/03-map"
121121
],
122122
"actions": [
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')"
123+
"insert('\nfunction increaseScore() {\n\n}\n\n// map over `mySlightlyChanged` with a function `increaseScore` to increment each score by 12\nvar mySlightlyChanged = myData;\n')"
124124
]
125125
},
126126
{
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.",
127+
"description": "Wait. Now you're getting 105 in \"Algorithm Design\" class. Fix `increaseScores` so that the maximum score is 95. That should be less suspicious.",
128128
"tests": [
129129
"1/03/04-map"
130-
],
131-
"actions": [
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')"
133130
]
134131
},
135132
{
@@ -138,7 +135,7 @@
138135
"1/03/05-map"
139136
],
140137
"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\n// set `myFixed` to update grades to the new scores\nvar myFixed = mySlightlyChanged;\n')"
138+
"insert('\n// change `getGrade` to accept an object\n// and return an object\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// map `myFixed` to update grades to the new scores\nvar myFixed = mySlightlyChanged;\n')"
142139
]
143140
},
144141
{
@@ -195,7 +192,7 @@
195192
},
196193
{
197194
"title": "find",
198-
"description": "Array -> find an item that matches a condition",
195+
"description": "Array -> first element that matches a condition",
199196
"explanation": "Somehow your name has disappeared from the computer system. We'll have to `find` a way to get it back.\n\nYou quickly put together a list of other students in class. If someone changed your name, it'll be the name that is not in that list.\n\n`find` works similar to `filter`, but returns only the first match.\n\n```\nvar data = [1, 2, 3, 4, 5, 6];\n\nfunction isEven(num) {\n return num % 2 === 0;\n}\n\n// returns all matching data to a condition\ndata.filter(isEven);\n//> [2, 4, 6]\n\n// returns the first match\ndata.find(isEven);\n//> [2]\n```\n\nFind is great for performantly matching unique values in data, such as \"id\"'s, or in our case, names.",
200197
"tasks": [
201198
{
@@ -300,7 +297,7 @@
300297
},
301298
{
302299
"title": "reduce",
303-
"description": "Array -> transform into anything",
300+
"description": "Array -> anything",
304301
"explanation": "```js\nfunction add(prev, curr) {\n console.log(`add(${prev}, ${curr}) -> ${prev + curr}`)\n return prev + curr;\n}\n\n[1, 5, 10].reduce(add, 100); // initial value\n\n// add(100, 1) -> 101\n// add(101, 5) -> 106\n// add(106, 10) -> 116\n//> 116\n```",
305302
"tasks": [
306303
{

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@ if (!global.myData) {
55
}
66
loadJS('03-map.js')
77

8-
describe('01 function changeGrades', function() {
8+
describe('01 function changeGrade', function() {
99

1010
it('doesn\'t exist', function() {
11-
expect(changeGrades).to.not.be.undefined;
11+
expect(changeGrade).to.not.be.undefined;
1212
});
1313

1414
it('isn\'t a function', function() {
15-
expect(changeGrades).to.be.a('function');
15+
expect(changeGrade).to.be.a('function');
1616
});
1717

1818
it('should take a parameter', function() {
19-
expect(changeGrades).to.have.length(1);
19+
expect(changeGrade).to.have.length(1);
2020
});
2121

2222
it('should try changing `student.grade` first before returning `student`', function () {
2323
var regex = /return [a-zA-Z]+\.grade/;
24-
var func = changeGrades.toString();
24+
var func = changeGrade.toString();
2525
expect(func.match(regex)).to.be.null;
2626
});
2727

2828
it('should change grades from a D to an A', function() {
2929
var test = {
3030
grade: 'D'
3131
};
32-
expect(changeGrades(test)).to.deep.equal({
32+
expect(changeGrade(test)).to.deep.equal({
3333
grade: 'A'
3434
});
3535
});
@@ -38,7 +38,7 @@ describe('01 function changeGrades', function() {
3838
var test = {
3939
grade: 'F'
4040
};
41-
expect(changeGrades(test)).to.deep.equal({
41+
expect(changeGrade(test)).to.deep.equal({
4242
grade: 'A'
4343
});
4444
});
@@ -47,7 +47,7 @@ describe('01 function changeGrades', function() {
4747
var test = {
4848
grade: 'B'
4949
};
50-
expect(changeGrades(test)).to.deep.equal({
50+
expect(changeGrade(test)).to.deep.equal({
5151
grade: 'A'
5252
});
5353
});

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

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

3-
describe('03 function increaseScores', function() {
3+
describe('03 function increaseScore', function() {
44

55
it('doesn\'t exist', function() {
6-
expect(increaseScores).to.not.be.undefined;
6+
expect(increaseScore).to.not.be.undefined;
77
});
88

99
it('should be a function', function() {
10-
expect(increaseScores).to.be.a('function');
10+
expect(increaseScore).to.be.a('function');
1111
});
1212

1313
it('should take a parameter', function() {
14-
expect(increaseScores).to.have.length(1);
14+
expect(increaseScore).to.have.length(1);
1515
});
1616

1717
it('should try changing the `score` first before returning the changed object', function() {
1818
var regex = /return [a-zA-Z]+\.score/;
19-
var func = increaseScores.toString();
19+
var func = increaseScore.toString();
2020
expect(func.match(regex)).to.be.null;
2121
});
2222

2323
it('should increment scores by 12 points', function() {
2424
var test = {
2525
score: 50
2626
};
27-
expect(increaseScores(test)).to.deep.equal({
27+
expect(increaseScore(test)).to.deep.equal({
2828
score: 62
2929
});
3030
});
@@ -42,10 +42,10 @@ describe('03 var mySlightlyChanged', function() {
4242
});
4343

4444
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]);
45+
var scores = mySlightlyChanged.map(function(x) {
46+
return x.score;
47+
});
48+
expect(Math.min.apply(Math, scores)).to.equal(70);
4949
});
5050

5151
});

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

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

3-
describe('04 function capScores', function() {
3+
describe('04 function increaseScore', function() {
44

55
it('doesn\'t exist', function() {
6-
expect(capScores).to.not.be.undefined;
6+
expect(increaseScore).to.not.be.undefined;
77
});
88

99
it('should be a function', function() {
10-
expect(capScores).to.be.a('function');
10+
expect(increaseScore).to.be.a('function');
1111
});
1212

1313
it('should take a parameter', function() {
14-
expect(capScores).to.have.length(1);
14+
expect(increaseScore).to.have.length(1);
1515
});
1616

17-
it('shouldn\'t change scores under 100', function() {
17+
it('shouldn\'t change scores under 95', function() {
1818
var test = {
19-
score: 99
19+
score: 82
2020
};
21-
expect(capScores(test)).to.deep.equal({
22-
score: 99
21+
expect(increaseScore(test)).to.deep.equal({
22+
score: 94
2323
});
2424
});
2525

26-
it('should change scores over 100 to 95', function() {
26+
it('should change scores over 95 to 95', function() {
2727
var test = {
28-
score: 101
28+
score: 84
2929
};
30-
expect(capScores(test)).to.deep.equal({
30+
expect(increaseScore(test)).to.deep.equal({
3131
score: 95
3232
});
3333
});
3434

3535
});
3636

37-
describe('04 var mySlightlyFixed', function() {
37+
describe('04 var mySlightlyChanged', function() {
3838

39-
it('doesn\'t exist', function() {
40-
expect(mySlightlyFixed).to.not.be.undefined;
41-
});
42-
43-
it('isn\'t an array', function() {
44-
expect(mySlightlyFixed).to.be.an('array');
45-
});
46-
47-
it('doesn\'t increment scores by 12', function() {
48-
expect(mySlightlyFixed).to.deep.equal([{
49-
"title": "Relational Databases",
50-
"instructor": "Sean Quentin Lewis",
51-
"name": "Ada Lovelace",
52-
"score": 95,
53-
"grade": "A"
54-
}, {
55-
"title": "3D Computer Graphics",
56-
"instructor": "G.L. Webb",
57-
"name": "Ada Lovelace",
58-
"score": 95,
59-
"grade": "B"
60-
}, {
61-
"title": "Front End Web Development",
62-
"instructor": "Moe Zaick",
63-
"name": "Ada Lovelace",
64-
"score": 73,
65-
"grade": "D"
66-
}, {
67-
"title": "Web Security",
68-
"instructor": "Sue Denim",
69-
"name": "Ada Lovelace",
70-
"score": 93,
71-
"grade": "B"
72-
}, {
73-
"title": "Javascript Fundamentals",
74-
"instructor": "Jay Kweerie",
75-
"name": "Ada Lovelace",
76-
"score": 85,
77-
"grade": "C"
78-
}, {
79-
"title": "Data Science",
80-
"instructor": "Ford Fulkerson",
81-
"name": "Ada Lovelace",
82-
"score": 70,
83-
"grade": "F"
84-
}, {
85-
"title": "Algorithm Design",
86-
"instructor": "Gale Shapely",
87-
"name": "Ada Lovelace",
88-
"score": 95,
89-
"grade": "A"
90-
}, {
91-
"title": "Data Abstraction",
92-
"instructor": "Aster Ricks",
93-
"name": "Ada Lovelace",
94-
"score": 94,
95-
"grade": "B"
96-
}, {
97-
"title": "Data Structures",
98-
"instructor": "Brodal Q.",
99-
"name": "Ada Lovelace",
100-
"score": 95,
101-
"grade": "B"
102-
}, {
103-
"title": "Networks",
104-
"instructor": "Van Emde Boas",
105-
"name": "Ada Lovelace",
106-
"score": 77,
107-
"grade": "D"
108-
}]);
39+
it('should cap scores at 95', function() {
40+
var scores = mySlightlyChanged.map(function(x) {
41+
return x.score;
42+
});
43+
expect(Math.max.apply(Math, scores)).to.equal(95);
10944
});
11045

11146
});

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

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ describe('05 function getGrade', function() {
1313
it('should take a parameter', function() {
1414
expect(getGrade).to.have.length(1);
1515
});
16+
17+
1618
});
1719

1820
describe('05 var myFixed', function() {
@@ -30,67 +32,9 @@ describe('05 var myFixed', function() {
3032
});
3133

3234
it('doesn\'t update grades correctly', function() {
33-
expect(myFixed).to.deep.equal([{
34-
"title": "Relational Databases",
35-
"instructor": "Sean Quentin Lewis",
36-
"name": "Ada Lovelace",
37-
"score": 95,
38-
"grade": "A"
39-
}, {
40-
"title": "3D Computer Graphics",
41-
"instructor": "G.L. Webb",
42-
"name": "Ada Lovelace",
43-
"score": 95,
44-
"grade": "A"
45-
}, {
46-
"title": "Front End Web Development",
47-
"instructor": "Moe Zaick",
48-
"name": "Ada Lovelace",
49-
"score": 73,
50-
"grade": "C"
51-
}, {
52-
"title": "Web Security",
53-
"instructor": "Sue Denim",
54-
"name": "Ada Lovelace",
55-
"score": 93,
56-
"grade": "A"
57-
}, {
58-
"title": "Javascript Fundamentals",
59-
"instructor": "Jay Kweerie",
60-
"name": "Ada Lovelace",
61-
"score": 85,
62-
"grade": "B"
63-
}, {
64-
"title": "Data Science",
65-
"instructor": "Ford Fulkerson",
66-
"name": "Ada Lovelace",
67-
"score": 70,
68-
"grade": "C"
69-
}, {
70-
"title": "Algorithm Design",
71-
"instructor": "Gale Shapely",
72-
"name": "Ada Lovelace",
73-
"score": 95,
74-
"grade": "A"
75-
}, {
76-
"title": "Data Abstraction",
77-
"instructor": "Aster Ricks",
78-
"name": "Ada Lovelace",
79-
"score": 94,
80-
"grade": "A"
81-
}, {
82-
"title": "Data Structures",
83-
"instructor": "Brodal Q.",
84-
"name": "Ada Lovelace",
85-
"score": 95,
86-
"grade": "A"
87-
}, {
88-
"title": "Networks",
89-
"instructor": "Van Emde Boas",
90-
"name": "Ada Lovelace",
91-
"score": 77,
92-
"grade": "C"
93-
}]);
35+
expect(myFixed.map(function(x) {
36+
return x.grade;
37+
})).to.deep.equal(['A', 'A', 'C', 'A', 'B', 'C', 'A', 'A', 'A', 'C']);
9438
});
9539

9640
});

0 commit comments

Comments
 (0)