1
1
<!DOCTYPE html>
2
2
< html lang ="en ">
3
+
3
4
< head >
4
5
< meta charset ="UTF-8 ">
5
6
< title > Array Cardio 💪</ title >
6
7
</ head >
8
+
7
9
< body >
8
- < p > < em > Psst: have a look at the JavaScript Console</ em > 💁</ p >
10
+ < p >
11
+ < em > Psst: have a look at the JavaScript Console</ em > 💁</ p >
9
12
< script >
10
13
// Get your shorts on - this is an array workout!
11
14
// ## Array Cardio Day 1
31
34
32
35
// Array.prototype.filter()
33
36
// 1. Filter the list of inventors for those who were born in the 1500's
37
+ const filteredInventors = inventors . filter ( inventor => inventor . year < 1600 && inventor . year >= 1500 ) ;
38
+ console . log ( 'filtered inventors' , filteredInventors ) ;
34
39
35
40
// Array.prototype.map()
36
41
// 2. Give us an array of the inventors' first and last names
42
+ const mappedInventors = inventors . map ( inventor => `${ inventor . first } ${ inventor . last } ` ) ;
43
+ console . log ( 'mapped inventors' , mappedInventors ) ;
37
44
38
45
// Array.prototype.sort()
39
46
// 3. Sort the inventors by birthdate, oldest to youngest
47
+ const sortedInventors = inventors . sort ( ( a , b ) => ( a . year > b . year ) ? 1 : - 1 ) ;
48
+ console . log ( 'sorted inventors' , sortedInventors ) ;
40
49
41
50
// Array.prototype.reduce()
42
51
// 4. How many years did all the inventors live?
52
+ const reducedInventors = inventors . reduce ( ( total , inventor ) => total + inventor . passed - inventor . year , 0 ) ;
53
+ console . log ( 'reduced inventors' , reducedInventors ) ;
54
+
43
55
44
56
// 5. Sort the inventors by years lived
57
+ const sortedInventorsByAge = inventors
58
+ . sort ( ( a , b ) => a . passed - a . year - b . passed + b . year )
59
+ . map ( item => {
60
+ let newItem = item ;
61
+ newItem . age = item . passed - item . year ;
62
+ return newItem ;
63
+ } ) ;
64
+ console . log ( 'sorted inventors by age' , sortedInventorsByAge ) ;
65
+
45
66
46
67
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
47
68
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
69
+ // Array.from(document.querySelectorAll('.mw-category a'))
70
+ // .map(item => item.innerText)
71
+ // .filter(item => item.match('de'));
48
72
49
73
50
74
// 7. sort Exercise
51
75
// Sort the people alphabetically by last name
76
+ const sortedPeople = people . sort ( ( a , b ) => a . split ( ', ' ) [ 1 ] > b . split ( ', ' ) [ 1 ] ? 1 : - 1 ) ;
77
+ console . log ( 'sorted people' , sortedPeople ) ;
78
+
52
79
53
80
// 8. Reduce Exercise
54
81
// Sum up the instances of each of these
55
- const data = [ 'car' , 'car' , 'truck' , 'truck' , 'bike' , 'walk' , 'car' , 'van' , 'bike' , 'walk' , 'car' , 'van' , 'car' , 'truck' ] ;
82
+ const data = [ 'car' , 'car' , 'truck' , 'truck' , 'bike' , 'walk' , 'car' , 'van' , 'bike' , 'walk' , 'car' , 'van' , 'car' , 'truck' ] ;
83
+
84
+ const transportation = data . reduce ( ( result , item ) => {
85
+ if ( ! result [ item ] ) {
86
+ result [ item ] = 0 ;
87
+ }
88
+ result [ item ] ++ ;
89
+ return result ;
90
+ } , { } ) ;
91
+ console . log ( 'transportation' , transportation ) ;
56
92
57
93
</ script >
58
94
</ body >
59
- </ html >
95
+
96
+ </ html >
0 commit comments