Skip to content

Commit 94cf7e7

Browse files
committed
06-concat progress
1 parent 111fe3d commit 94cf7e7

File tree

5 files changed

+155
-26
lines changed

5 files changed

+155
-26
lines changed

coderoad.json

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,25 +225,55 @@
225225
]
226226
},
227227
{
228-
"description": "First, test out `flatten` on the `flattenedArray`\n @test('1/06/01-concat')\n @open('06-concat.js')\n @action(set(\n```\n// Array.prototype can be used to create new Array methods\nArray.prototype.flatten = function() {\n // more on `reduce` coming up next\n return this.reduce(function(a, b) {\n return a.concat(b);\n }, []);\n});\n``` \n))",
228+
"description": "First, test out `flatten` on the `flattenedArray`\n@open('06-concat.js')",
229+
"tests": [
230+
"1/06/01-concat"
231+
],
232+
"hints": [
233+
"js\nvar start = [{\n a: 1,\n c: [\n { b: 1 }\n ]\n}, {\n a: 2,\n c: [\n { b: 2 }, { b: 3 }\n ]\n}];\n\nvar middle = start.map(function(outer) {\n return outer.c.map(function(inner) {\n return {\n a: outer.a,\n b: inner.b\n };\n });\n});\n//> [ [{ a: 1, b: 1 }], [{a: 2, b: 2}, {a: 2, b: 3}] ]",
234+
"Flatten the resulting arrays",
235+
"js\nvar end = pre.flatten();\n//> [{a: 1, b: 1}, {a: 2, b: 2}, {a: 2, b: 3}]"
236+
],
229237
"actions": [
230-
"insert('// use `flatten` to set flattenedArray to [1, 2, 3, 4]\nvar flattenedArray = [[1, 2], [3, 4]];\n')"
238+
"set('`\n// Array.prototype can be used to create new Array methods\nArray.prototype.flatten = function() {\n // more on `reduce` coming up next\n return this.reduce(function(a, b) {\n return a.concat(b);\n }, []);\n});')",
239+
"insert('\n// use `flatten` on `numberedList` to set flattenedArray to [1, 2, 3, 4]\nvar numberedList = [[1, 2], [3, 4]]\nvar flattenedArray;\n')"
231240
]
232241
},
233242
{
234243
"description": "Map over the course data array, and map over the student array inside of the course data. Set `doubleArray` to return an array of array of objects that looks like this:\n\n```js\n {\n \"title\": \"Relational Databases\",\n \"instructor\": \"Sean Quentin Lewis\",\n \"name\": \"Rebecca Heineman\",\n \"score\": 71,\n \"grade\": \"C\"\n }\n```",
235244
"tests": [
236245
"1/06/02-concat"
246+
],
247+
"actions": [
248+
"insert('\n// map over doubleArray twice\nvar doubleArray = courses;\n')"
237249
]
238250
},
239251
{
240-
"description": "Use `flatten` to put all data into a single array. Set `flattened` to the result.\n @test('1/06/03-concat')"
252+
"description": "Use `flatten` to put all data into a single array. Set `students` to the result.",
253+
"tests": [
254+
"1/06/03-concat"
255+
],
256+
"actions": [
257+
"insert('\n// flatten doubleArray\nvar students = doubleArray;\n')"
258+
]
241259
},
242260
{
243-
"description": "Use the `suspects` array to `filter` down \"Hack Kerr\"'s data\n @test('1/06/04-concat')"
261+
"description": "Use the `suspects` array to `filter` to only \"Hack Kerr\"'s data",
262+
"tests": [
263+
"1/06/04-concat"
264+
],
265+
"actions": [
266+
"insert('\nvar suspects = [\"Hack Kerr\"];\n\n// filter to data matching `suspects`\nvar suspectData = students;\n')"
267+
]
244268
},
245269
{
246-
"description": "You just thought of two more suspects: `concat` two more suspects to the suspect list, \"Kevin Mitnick\" & \"Albert Gonzalez\"\n @test('1/06/05-concat')"
270+
"description": "You just thought of two more suspects! `concat` the new suspects array onto the `suspects` list.",
271+
"tests": [
272+
"1/06/05-concat"
273+
],
274+
"actions": [
275+
"insert('\nvar newSuspects = [\"Kevin Mitnick\", \"Albert Gonzalez\"];\n')"
276+
]
247277
}
248278
]
249279
},

tutorial/1/06/02-concat.spec.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,28 @@ if (!global.courses) {
1010
global.courses = JSON.parse(JSON.stringify(require('../../data/courses2.json')));
1111
}
1212

13-
describe('var flattenedArray', function() {
13+
describe('var doubleArray', function() {
1414

15-
var spy = chai.spy.on(Array, 'flatten');
15+
var spy = chai.spy.on(Array, 'map');
1616
loadJS('06-concat.js');
1717

18-
it('should flatten the array', function() {
19-
expect(flattenedArray).to.deep.equal([1, 2, 3, 4]);
18+
it('should call map twice', function() {
19+
expect(spy).to.have.been.called.twice;
2020
});
2121

22-
it('should use the `flatten` Array method', function() {
23-
expect(spy).to.have.been.called();
22+
it('should create an array of arrays', function() {
23+
var result = courses.map(function(course) {
24+
return course.students.map(function(student) {
25+
return {
26+
title: course.title,
27+
instructor: course.instructor,
28+
name: student.name,
29+
score: student.score,
30+
grade: student.grade
31+
};
32+
});
33+
});
34+
expect(doubleArray).to.deep.equal(result);
2435
});
2536

2637
});

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,29 @@ if (!global.courses) {
1010
global.courses = JSON.parse(JSON.stringify(require('../../data/courses2.json')));
1111
}
1212

13-
describe('var flattenedArray', function() {
13+
describe('var students', function() {
14+
15+
var spy = chai.spy.on(Array, 'map');
16+
loadJS('06-concat.js');
17+
18+
it('should flatten the array with `.flatten()`', function() {
19+
expect(spy).to.have.been.called.twice;
20+
});
21+
22+
it('should result in a single array of student data', function () {
23+
var result = courses.map(function(course) {
24+
return course.students.map(function(student) {
25+
return {
26+
title: course.title,
27+
instructor: course.instructor,
28+
name: student.name,
29+
score: student.score,
30+
grade: student.grade
31+
};
32+
});
33+
}).flatten();
34+
expect(students).to.deep.equal(result);
35+
})
36+
1437

15-
1638
});

tutorial/1/06/04-concat.spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ if (!global.courses) {
1010
global.courses = JSON.parse(JSON.stringify(require('../../data/courses2.json')));
1111
}
1212

13-
describe('var flattenedArray', function() {
13+
describe('var suspectData', function() {
1414

15+
it('should filter if the `indexOf` the suspects name is greater than -1', function() {
16+
17+
});
1518

1619
});

tutorial/1/06/concat.md

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,41 @@ We have a suspect in mind: a classmate named "Hack Kerr". He's a nice guy, and h
9292
First let's recreate our student array of data from the course data.
9393

9494
+ First, test out `flatten` on the `flattenedArray`
95-
@test('1/06/01-concat')
96-
@open('06-concat.js')
97-
@action(set(
95+
@test('1/06/01-concat')
96+
@open('06-concat.js')
97+
@hint(
98+
```js
99+
var start = [{
100+
a: 1,
101+
c: [
102+
{ b: 1 }
103+
]
104+
}, {
105+
a: 2,
106+
c: [
107+
{ b: 2 }, { b: 3 }
108+
]
109+
}];
110+
111+
var middle = start.map(function(outer) {
112+
return outer.c.map(function(inner) {
113+
return {
114+
a: outer.a,
115+
b: inner.b
116+
};
117+
});
118+
});
119+
//> [ [{ a: 1, b: 1 }], [{a: 2, b: 2}, {a: 2, b: 3}] ]
120+
```
121+
)
122+
@hint(Flatten the resulting arrays)
123+
@hint(
124+
```js
125+
var end = pre.flatten();
126+
//> [{a: 1, b: 1}, {a: 2, b: 2}, {a: 2, b: 3}]
127+
```
128+
)
129+
@action(set(
98130
```
99131
// Array.prototype can be used to create new Array methods
100132
Array.prototype.flatten = function() {
@@ -104,11 +136,13 @@ Array.prototype.flatten = function() {
104136
}, []);
105137
});
106138
```
107-
))
139+
)
108140
@action(insert(
109141
```
110-
// use `flatten` to set flattenedArray to [1, 2, 3, 4]
111-
var flattenedArray = [[1, 2], [3, 4]];
142+
143+
// use `flatten` on `numberedList` to set flattenedArray to [1, 2, 3, 4]
144+
var numberedList = [[1, 2], [3, 4]]
145+
var flattenedArray;
112146
```
113147
))
114148

@@ -124,12 +158,41 @@ var flattenedArray = [[1, 2], [3, 4]];
124158
}
125159
```
126160
@test('1/06/02-concat')
161+
@action(insert(
162+
```
127163
128-
+ Use `flatten` to put all data into a single array. Set `flattened` to the result.
129-
@test('1/06/03-concat')
164+
// map over doubleArray twice
165+
var doubleArray = courses;
166+
```
167+
))
168+
169+
+ Use `flatten` to put all data into a single array. Set `students` to the result.
170+
@test('1/06/03-concat')
171+
@action(insert(
172+
```
130173
131-
+ Use the `suspects` array to `filter` down "Hack Kerr"'s data
132-
@test('1/06/04-concat')
174+
// flatten doubleArray
175+
var students = doubleArray;
176+
```
177+
))
133178

134-
+ You just thought of two more suspects: `concat` two more suspects to the suspect list, "Kevin Mitnick" & "Albert Gonzalez"
135-
@test('1/06/05-concat')
179+
+ Use the `suspects` array to `filter` to only "Hack Kerr"'s data
180+
@test('1/06/04-concat')
181+
@action(insert(
182+
```
183+
184+
var suspects = ["Hack Kerr"];
185+
186+
// filter to data matching `suspects`
187+
var suspectData = students;
188+
```
189+
))
190+
191+
+ You just thought of two more suspects! `concat` the new suspects array onto the `suspects` list.
192+
@test('1/06/05-concat')
193+
@action(insert(
194+
```
195+
196+
var newSuspects = ["Kevin Mitnick", "Albert Gonzalez"];
197+
```
198+
))

0 commit comments

Comments
 (0)