Skip to content

Commit 854ecd0

Browse files
committed
step 5 updated
1 parent d50e8a3 commit 854ecd0

File tree

10 files changed

+82
-34
lines changed

10 files changed

+82
-34
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ Array -> run a function for each item
4848

4949
Array -> first element that matches a condition
5050

51+
##### concat
52+
53+
Array + Array -> Array
54+
5155
##### reduce
5256

5357
Array -> anything

coderoad.json

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@
193193
{
194194
"title": "find",
195195
"description": "Array -> first element that matches a condition",
196-
"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.",
196+
"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 an \"id\", or in our case: a name.",
197197
"tasks": [
198198
{
199199
"description": "`filter` to students in the class titled \"Web Security\"",
@@ -202,8 +202,7 @@
202202
],
203203
"actions": [
204204
"open('05-find.js')",
205-
"set('// search for a student with a name\n// not matching students in the list\nvar otherStudents = [\"Albert Gonzalez\", \"Brian Kernaghan\", \"Danielle Bunten Berry\", \"Donald Knuth\", \"Grace Hopper\", \"Hack Kerr\", \"James Gosling\", \"Ken Thompson\", \"Kevin Mitnick\", \"Linus Torvalds\", \"Niklaus Wirth\", \"Rebecca Heineman\", \"Tim Berners-Lee\", \"Xiao Tian\", \"Ying Cracker\"];\n')",
206-
"insert('\n// filter for students.title is \"Web Security\"\nvar myClass = students\n')"
205+
"set('// filter for students.title is \"Web Security\"\nvar myClass = students.filter();\n')"
207206
]
208207
},
209208
{
@@ -212,7 +211,8 @@
212211
"1/05/02-find"
213212
],
214213
"actions": [
215-
"insert('\n// hint: use `indexOf` to find if an item is in the array\nvar unknownStudent \n')"
214+
"insert('\n// search for a student with a name\n// not matching students in the list\nvar otherStudents = [\"Albert Gonzalez\", \"Brian Kernaghan\", \"Danielle Bunten Berry\", \"Donald Knuth\", \"Grace Hopper\", \"Hack Kerr\", \"James Gosling\", \"Ken Thompson\", \"Kevin Mitnick\", \"Linus Torvalds\", \"Niklaus Wirth\", \"Rebecca Heineman\", \"Tim Berners-Lee\", \"Xiao Tian\", \"Ying Cracker\"];\n\n')",
215+
"insert('// hint: use `indexOf` to find if an item is in the array\nfunction notInList() {\n\n}\n\n// find using `notInList`\nvar unknownStudent = myClass.find();\n')"
216216
]
217217
},
218218
{
@@ -221,7 +221,7 @@
221221
"1/05/03-find"
222222
],
223223
"actions": [
224-
"insert('var unknownStudentList\n')"
224+
"insert('\n// filter using `notInList`\nvar unknownStudentList = students.filter();\n')"
225225
]
226226
},
227227
{
@@ -230,18 +230,31 @@
230230
"1/05/04-find"
231231
],
232232
"actions": [
233-
"insert('var unknownStudentNames\n')"
233+
"insert('\n// use `map` to return only the `student.name`\nvar unknownStudentNames = unknownStudentList.map();\n')"
234234
]
235235
},
236236
{
237-
"description": "`join('')` the array of names to output result\n\n# concat\nArray + Array -> Array\n\nBefore we've been working on a structured set of student data.\n\n```js\n// array of students\n[\n {\n \"title\": \"Relational Databases\",\n \"instructor\": \"Sean Quentin Lewis\",\n \"name\": \"Rebecca Heineman\",\n \"score\": 71,\n \"grade\": \"C\"\n }\n// students in courses...\n]\n```\n\nTo be safe, let's now work on the original data set. Notice how it is structured differently.\n\n```js\n// array of courses\n[\n {\n \"title\": \"Relational Databases\",\n \"instructor\": \"Sean Quentin Lewis\",\n \"students\": [\n {\n \"name\": \"Rebecca Heineman\",\n \"score\": 71,\n \"grade\": \"C\"\n }\n // students...\n ]\n }\n // courses...\n]\n```\n\nIn this data set, there is an array of students within an array of courses. So how can we recreate our original array of students from the courses? We can use `concat`.\n\nWeird things happen when you start combining arrays. `concat` brings sanity.\n\n```js\n[1, 2] + [3, 4];\n//> \"1, 23, 4\"\n\n[1, 2].push([3, 4]);\n//> 3\n\n[1, 2].join([3, 4]);\n//> \"13, 42\"\n\n[1, 2].concat([3, 4]);\n//> [1, 2, 3, 4]\n```\n\nUnfortunately, Javascript is missing a built in array method to concat multiple arrays together: let's call it `flatten` (often called `concatAll`). `flatten` should loop over an array and concat each element.\n\n```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}] ]\n\nvar end = pre.flatten();\n//> [{a: 1, b: 1}, {a: 2, b: 2}, {a: 2, b: 3}]\n```\n\nBack to business.\n\nWe have a suspect in mind: a classmate named \"Hack Kerr\". He's a nice guy, and he's always been friendly to you - but there's something suspicious about him: his name.\n\nFirst let's recreate our student array of data from the course data.",
237+
"description": "`join('')` the array of names to output the result as a string",
238238
"tests": [
239239
"1/05/05-find"
240240
],
241241
"actions": [
242-
"insert('var decodedName\n')"
242+
"insert('\n// use `.join('')` to join the array of strings\nvar decodedName = unknownStudentNames;\nconsole.log(decodedName);\n')"
243243
]
244244
},
245+
{
246+
"description": "Very strange. In the next step, let's find out who wants revenge, and give it to him!",
247+
"tests": [
248+
"1/05/06-find"
249+
]
250+
}
251+
]
252+
},
253+
{
254+
"title": "concat",
255+
"description": "Array + Array -> Array",
256+
"explanation": "Before we've been working on a structured set of student data.\n\n```js\n// array of students\n[\n {\n \"title\": \"Relational Databases\",\n \"instructor\": \"Sean Quentin Lewis\",\n \"name\": \"Rebecca Heineman\",\n \"score\": 71,\n \"grade\": \"C\"\n }\n// students in courses...\n]\n```\n\nTo be safe, let's now work on the original data set. Notice how it is structured differently.\n\n```js\n// array of courses\n[\n {\n \"title\": \"Relational Databases\",\n \"instructor\": \"Sean Quentin Lewis\",\n \"students\": [\n {\n \"name\": \"Rebecca Heineman\",\n \"score\": 71,\n \"grade\": \"C\"\n }\n // students...\n ]\n }\n // courses...\n]\n```\n\nIn this data set, there is an array of students within an array of courses. So how can we recreate our original array of students from the courses? We can use `concat`.\n\nWeird things happen when you start combining arrays. `concat` brings sanity.\n\n```js\n[1, 2] + [3, 4];\n//> \"1, 23, 4\"\n\n[1, 2].push([3, 4]);\n//> 3\n\n[1, 2].join([3, 4]);\n//> \"13, 42\"\n\n[1, 2].concat([3, 4]);\n//> [1, 2, 3, 4]\n```\n\nUnfortunately, Javascript is missing a built in array method to concat multiple arrays together: let's call it `flatten` (often called `concatAll`). `flatten` should loop over an array and concat each element.\n\n```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}] ]\n\nvar end = pre.flatten();\n//> [{a: 1, b: 1}, {a: 2, b: 2}, {a: 2, b: 3}]\n```\n\nBack to business.\n\nWe have a suspect in mind: a classmate named \"Hack Kerr\". He's a nice guy, and he's always been friendly to you - but there's something suspicious about him: his name.\n\nFirst let's recreate our student array of data from the course data.",
257+
"tasks": [
245258
{
246259
"description": "First, test out `flatten` on the `flattenedArray`\n@open('06-concat.js')",
247260
"tests": [

tutorial/1/05/01-find.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
var expect = require('chai').expect;
33
var spies = require('chai-spies');
4-
var path = require('path');
54
var loadJS = require('./common/loadJS').default;
65

76
if (!global.students) {

tutorial/1/05/02-find.spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
describe('02 function notInList', function() {
2+
3+
it('should filter for student.name', function() {
4+
var regex = /[a-zA-Z]+\.name/;
5+
var str = notInList.toString();
6+
expect(str.match(regex)).to.not.be.null;
7+
});
8+
9+
});
10+
111
describe('02 var unknownStudent', function() {
212

313
var otherStudents = ["Albert Gonzalez", "Brian Kernaghan", "Danielle Bunten Berry", "Donald Knuth", "Grace Hopper", "Hack Kerr", "James Gosling", "Ken Thompson", "Kevin Mitnick", "Linus Torvalds", "Niklaus Wirth", "Rebecca Heineman", "Tim Berners-Lee", "Xiao Tian", "Ying Cracker"];
414

5-
it('should filter to "Web Security" class data', function () {
15+
it('should filter to "Web Security" class data', function() {
616
expect(unknownStudent.name).to.equal('he');
717
});
818

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('03 var unknownStudentList', function() {
55
});
66

77
it('should find 10 unknown students across classes', function() {
8-
let names = unknownStudentList.map(function(student) {
8+
var names = unknownStudentList.map(function(student) {
99
return student.name;
1010
}).join('');
1111
expect(names).to.equal('!findthebestrevenge!');

tutorial/1/05/04-find.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
describe('04 var unknownStudentNames', function() {
22

33
it('should find 10 unknown students names', function() {
4-
let names = unknownStudentNames.join('');
4+
var names = unknownStudentNames.join('');
55
expect(names).to.equal('!findthebestrevenge!');
66
});
77

tutorial/1/05/06-find.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
describe('06 complete', function() {
2+
3+
it('should pass', function() {
4+
expect(true).to.be.true;
5+
});
6+
7+
});

tutorial/1/05/find.md

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,15 @@ data.find(isEven);
2323
//> [2]
2424
```
2525

26-
Find is great for performantly matching unique values in data, such as "id"'s, or in our case, names.
26+
Find is great for performantly matching unique values in data, such as an "id", or in our case: a name.
2727

2828
+ `filter` to students in the class titled "Web Security"
2929
@test('1/05/01-find')
3030
@action(open('05-find.js'))
3131
@action(set(
3232
```
33-
// search for a student with a name
34-
// not matching students in the list
35-
var otherStudents = ["Albert Gonzalez", "Brian Kernaghan", "Danielle Bunten Berry", "Donald Knuth", "Grace Hopper", "Hack Kerr", "James Gosling", "Ken Thompson", "Kevin Mitnick", "Linus Torvalds", "Niklaus Wirth", "Rebecca Heineman", "Tim Berners-Lee", "Xiao Tian", "Ying Cracker"];
36-
```
37-
))
38-
@action(insert(
39-
```
40-
4133
// filter for students.title is "Web Security"
42-
var myClass = students
34+
var myClass = students.filter();
4335
```
4436
))
4537

@@ -48,31 +40,54 @@ var myClass = students
4840
@action(insert(
4941
```
5042
43+
// search for a student with a name
44+
// not matching students in the list
45+
var otherStudents = ["Albert Gonzalez", "Brian Kernaghan", "Danielle Bunten Berry", "Donald Knuth", "Grace Hopper", "Hack Kerr", "James Gosling", "Ken Thompson", "Kevin Mitnick", "Linus Torvalds", "Niklaus Wirth", "Rebecca Heineman", "Tim Berners-Lee", "Xiao Tian", "Ying Cracker"];
46+
47+
```
48+
))
49+
@action(insert(
50+
```
5151
// hint: use `indexOf` to find if an item is in the array
52-
var unknownStudent
52+
function notInList() {
53+
54+
}
55+
56+
// find using `notInList`
57+
var unknownStudent = myClass.find();
5358
```
5459
))
5560

5661
+ `filter` down to students without known names
5762
@test('1/05/03-find')
5863
@action(insert(
5964
```
60-
var unknownStudentList
65+
66+
// filter using `notInList`
67+
var unknownStudentList = students.filter();
6168
```
6269
))
6370

6471
+ `map` over the result to get only the `name` property
6572
@test('1/05/04-find')
6673
@action(insert(
6774
```
68-
var unknownStudentNames
75+
76+
// use `map` to return only the `student.name`
77+
var unknownStudentNames = unknownStudentList.map();
6978
```
7079
))
7180

72-
+ `join('')` the array of names to output result
81+
+ `join('')` the array of names to output the result as a string
7382
@test('1/05/05-find')
7483
@action(insert(
7584
```
76-
var decodedName
85+
86+
// use `.join('')` to join the array of strings
87+
var decodedName = unknownStudentNames;
88+
console.log(decodedName);
7789
```
7890
))
91+
92+
+ Very strange. In the next step, let's find out who wants revenge, and give it to him!
93+
@test('1/05/06-find')

tutorial/1/06/concat.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# concat
1+
### concat
22
Array + Array -> Array
33

44
Before we've been working on a structured set of student data.

tutorial/data/students2.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@
289289
}, {
290290
"title": "Web Security",
291291
"instructor": "Sue Denim",
292-
"name": "he",
293-
"score": 81,
294-
"grade": "B"
292+
"name": "Donald Knuth",
293+
"score": 44,
294+
"grade": "F"
295295
}, {
296296
"title": "Web Security",
297297
"instructor": "Sue Denim",
@@ -313,9 +313,9 @@
313313
}, {
314314
"title": "Web Security",
315315
"instructor": "Sue Denim",
316-
"name": "Donald Knuth",
317-
"score": 44,
318-
"grade": "F"
316+
"name": "he",
317+
"score": 81,
318+
"grade": "B"
319319
}, {
320320
"title": "Web Security",
321321
"instructor": "Sue Denim",

0 commit comments

Comments
 (0)