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 >
34
36
'Berne, Eric' , 'Berra, Yogi' , 'Berry, Wendell' , 'Bevan, Aneurin' , 'Ben-Gurion, David' , 'Bevel, Ken' , 'Biden, Joseph' , 'Bennington, Chester' , 'Bierce, Ambrose' ,
35
37
'Billings, Josh' , 'Birrell, Augustine' , 'Blair, Tony' , 'Beecher, Henry' , 'Biondo, Frank'
36
38
] ;
37
-
39
+
38
40
// Array.prototype.filter()
39
41
// 1. Filter the list of inventors for those who were born in the 1500's
42
+ //filter returns the only results where the condition is satisfied
43
+ console . table ( inventors . filter ( inventor => inventor . year >= 1500 && inventor . year <= 1599 ) )
40
44
41
45
// Array.prototype.map()
42
46
// 2. Give us an array of the inventors first and last names
47
+ // map returns the array equal to the size of the parent array
48
+ console . log ( inventors . map ( inventor => `${ inventor . first } ${ inventor . last } ` ) )
43
49
44
- // Array.prototype .sort()
50
+ // Array.prot otype .sort()
45
51
// 3. Sort the inventors by birthdate, oldest to youngest
52
+ console . table ( inventors . sort ( ( a , b ) => a . year > b . year ? 1 : - 1 ) ) ;
46
53
47
54
// Array.prototype.reduce()
48
55
// 4. How many years did all the inventors live all together?
56
+ const totalYears = inventors . reduce ( ( total , inventor ) => {
57
+ return total + ( inventor . passed - inventor . year ) ;
58
+ } , 0 ) ;
59
+ console . log ( totalYears ) ;
49
60
50
61
// 5. Sort the inventors by years lived
62
+ const oldest = inventors . sort ( ( a , b ) => {
63
+ const A = a . passed - a . year ;
64
+ const B = b . passed - b . year ;
65
+ return A > B ? - 1 : 1 ;
66
+ } )
67
+ console . table ( oldest ) ;
51
68
52
69
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
53
70
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
54
-
71
+ // const category = document.querySelector('.mw-category');
72
+ // const links = Array.from(category.querySelectorAll('a'));
73
+ // const de = links
74
+ // .map(link => link.textContent)
75
+ // .filter(streetName => streetName.includes("de"));
76
+ // console.log(de);
55
77
56
78
// 7. sort Exercise
57
79
// Sort the people alphabetically by last name
80
+ const alpha = people . sort ( function ( lastOne , nextOne ) {
81
+ const [ aLast , aFirst ] = lastOne . split ( ', ' ) ;
82
+ const [ bLast , bFirst ] = nextOne . split ( ', ' ) ;
83
+ return aLast > bLast ? - 1 : 1 ;
84
+ } )
85
+ console . log ( alpha ) ;
58
86
59
87
// 8. Reduce Exercise
60
88
// Sum up the instances of each of these
61
- const data = [ 'car' , 'car' , 'truck' , 'truck' , 'bike' , 'walk' , 'car' , 'van' , 'bike' , 'walk' , 'car' , 'van' , 'car' , 'truck' ] ;
89
+ const data = [ 'car' , 'car' , 'truck' , 'truck' , 'bike' , 'walk' , 'car' , 'van' , 'bike' , 'walk' , 'car' , 'van' , 'car' , 'truck' ] ;
90
+
91
+ const transportation = data . reduce ( ( obj , item ) => {
92
+ if ( ! obj [ item ] ) {
93
+ obj [ item ] = 0 ;
94
+ }
95
+ obj [ item ] ++ ;
96
+ return obj ;
97
+ } , { } ) ;
98
+
99
+ console . log ( transportation ) ;
62
100
63
101
</ script >
64
102
</ body >
65
- </ html >
103
+
104
+ </ html >
0 commit comments