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
< script >
9
11
// Get your shorts on - this is an array workout!
27
29
28
30
// Array.prototype.filter()
29
31
// 1. Filter the list of inventors for those who were born in the 1500's
32
+ const inventors_from_1500s = inventors . filter ( ( inventor ) => inventor . year >= 1500 && inventor . year < 1600 )
33
+ console . table ( inventors_from_1500s )
30
34
31
35
// Array.prototype.map()
32
- // 2. Give us an array of the inventory first and last names
36
+ // 2. Give us an array of the inventors first and last names
37
+ const inventors_full_name = inventors . map ( ( inventor ) => `${ inventor . first } ${ inventor . last } ` )
38
+ console . table ( inventors_full_name )
33
39
34
40
// Array.prototype.sort()
35
41
// 3. Sort the inventors by birthdate, oldest to youngest
42
+ const inventors_by_date = inventors . sort ( ( a , b ) => a . year - b . year )
43
+ console . table ( inventors_by_date )
36
44
37
45
// Array.prototype.reduce()
38
46
// 4. How many years did all the inventors live?
39
-
47
+ const total_years_lived = inventors . reduce ( ( currValue , inventor ) => currValue += ( inventor . passed - inventor . year ) , 0 )
48
+ console . log ( total_years_lived ) ;
40
49
// 5. Sort the inventors by years lived
50
+ const inventors_by_lifespan = inventors . sort ( ( a , b ) => ( a . passed - a . year ) - ( b . passed - b . year ) ) ;
51
+ console . table ( inventors_by_lifespan ) ;
41
52
42
53
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
43
54
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
44
-
55
+ /*
56
+ This code will be run directly from the browser console
57
+ after navigating to the Wikipedia link above.
58
+
59
+ const blvd_with_de = Array.from(document.querySelectorAll('.mw-category a'))
60
+ .map(link => link.textContent)
61
+ .filter((blvd) => blvd.includes('de'))
62
+ */
45
63
46
64
// 7. sort Exercise
47
65
// Sort the people alphabetically by last name
66
+ const people_by_last_name = people . sort (
67
+ ( a , b ) => a . split ( ', ' ) [ 0 ] > b . split ( ', ' ) [ 0 ] ? - 1 : 1 ) ;
48
68
69
+ console . table ( people_by_last_name ) ;
49
70
// 8. Reduce Exercise
50
71
// Sum up the instances of each of these
51
- const data = [ 'car' , 'car' , 'truck' , 'truck' , 'bike' , 'walk' , 'car' , 'van' , 'bike' , 'walk' , 'car' , 'van' , 'car' , 'truck' ] ;
72
+ const data = [ 'car' , 'car' , 'truck' , 'truck' , 'bike' , 'walk' , 'car' , 'van' , 'bike' , 'walk' , 'car' , 'van' , 'car' , 'truck' ] ;
73
+
74
+ const data_instance_count = data . reduce ( ( tallyObject , item ) => {
75
+ if ( ! tallyObject [ item ] ) {
76
+ tallyObject [ item ] = 0 ;
77
+ }
78
+ tallyObject [ item ] ++ ;
79
+ return tallyObject ;
80
+ } , { } ) ;
81
+
82
+ console . table ( data_instance_count ) ;
52
83
53
84
</ script >
54
85
</ body >
55
- </ html >
86
+
87
+ </ html >
0 commit comments