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
10
< p > < em > Psst: have a look at the JavaScript Console</ em > 💁</ p >
9
11
< script >
32
34
// Array.prototype.filter()
33
35
// 1. Filter the list of inventors for those who were born in the 1500's
34
36
37
+ const filterList = inventors . filter ( i => i . year >= 1500 && i . year < 1600 ) ;
38
+ console . log ( 'Inventors born in the 1500 : ' ) ;
39
+ console . table ( filterList ) ;
40
+
35
41
// Array.prototype.map()
36
42
// 2. Give us an array of the inventors' first and last names
37
43
44
+ const mapList = inventors . map ( i => i . first + ' ' + i . last ) ;
45
+ console . log ( 'Inventors names : ' ) ;
46
+ console . log ( mapList ) ;
47
+
48
+
38
49
// Array.prototype.sort()
39
50
// 3. Sort the inventors by birthdate, oldest to youngest
40
51
52
+ const sortList = inventors . sort ( ( a , b ) => a . year > b . year ? 1 : - 1 ) ;
53
+ console . log ( 'Inventors birthdates : ' ) ;
54
+ console . table ( sortList ) ;
55
+
41
56
// Array.prototype.reduce()
42
57
// 4. How many years did all the inventors live?
58
+ const yearsLived = inventors . reduce ( ( t , i ) => {
59
+ return t + ( i . passed - i . year )
60
+ } , 0 ) ;
61
+ console . log ( "Inventors lived " + yearsLived + " years." ) ;
62
+ console . log ( yearsLived ) ;
43
63
44
64
// 5. Sort the inventors by years lived
65
+ const sortedByYearsLived = inventors . sort (
66
+ ( a , b ) => ( a . passed - a . year ) > ( b . passed - b . year ) ? 1 : - 1 ) ;
67
+ console . log ( "Inventors by years lived: " ) ;
68
+ console . table ( sortedByYearsLived ) ;
45
69
46
70
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
47
71
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
48
-
72
+ /*
73
+ const category = document.querySelector('.mw-category');
74
+ const links = [...category.querySelectorAll('a')];
75
+ const de = links
76
+ .map(link => link.text)
77
+ .filter(streetName => streetName.includes('de'));
78
+ console.log(de);
79
+ */
49
80
50
81
// 7. sort Exercise
51
82
// Sort the people alphabetically by last name
83
+ const sortedByName = people . sort (
84
+ ( a , b ) => a . split ( ', ' ) [ 0 ] > b . split ( ', ' ) [ 0 ] ? 1 : - 1 ) ;
85
+ console . log ( "People sorted by name : " )
86
+ console . log ( sortedByName ) ;
87
+
52
88
53
89
// 8. Reduce Exercise
54
90
// 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' ] ;
91
+ const data = [ 'car' , 'car' , 'truck' , 'truck' , 'bike' , 'walk' , 'car' , 'van' , 'bike' , 'walk' , 'car' , 'van' , 'car' , 'truck' ] ;
56
92
93
+ const reducedData = data . reduce ( ( obj , count ) => {
94
+ obj [ count ] ? obj [ count ] ++ : obj [ count ] = 1 ;
95
+ return obj ;
96
+ } , { } ) ;
97
+
98
+ console . log ( reducedData ) ;
57
99
</ script >
58
100
</ body >
59
- </ html >
101
+
102
+ </ html >
0 commit comments