1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+
4+ < head >
5+ < meta charset ="UTF-8 ">
6+ < title > Array Cardio 💪</ title >
7+ </ head >
8+
9+ < body >
10+ < p >
11+ < em > Psst: have a look at the JavaScript Console</ em > 💁</ p >
12+ < script >
13+ // Get your shorts on - this is an array workout!
14+ // ## Array Cardio Day 1
15+
16+ // Some data we can work with
17+
18+ const inventors = [
19+ { first : 'Albert' , last : 'Einstein' , year : 1879 , passed : 1955 } ,
20+ { first : 'Isaac' , last : 'Newton' , year : 1643 , passed : 1727 } ,
21+ { first : 'Galileo' , last : 'Galilei' , year : 1564 , passed : 1642 } ,
22+ { first : 'Marie' , last : 'Curie' , year : 1867 , passed : 1934 } ,
23+ { first : 'Johannes' , last : 'Kepler' , year : 1571 , passed : 1630 } ,
24+ { first : 'Nicolaus' , last : 'Copernicus' , year : 1473 , passed : 1543 } ,
25+ { first : 'Max' , last : 'Planck' , year : 1858 , passed : 1947 } ,
26+ { first : 'Katherine' , last : 'Blodgett' , year : 1898 , passed : 1979 } ,
27+ { first : 'Ada' , last : 'Lovelace' , year : 1815 , passed : 1852 } ,
28+ { first : 'Sarah E.' , last : 'Goode' , year : 1855 , passed : 1905 } ,
29+ { first : 'Lise' , last : 'Meitner' , year : 1878 , passed : 1968 } ,
30+ { first : 'Hanna' , last : 'Hammarström' , year : 1829 , passed : 1909 }
31+ ] ;
32+
33+ const people = [ 'Beck, Glenn' , 'Becker, Carl' , 'Beckett, Samuel' , 'Beddoes, Mick' , 'Beecher, Henry' , 'Beethoven, Ludwig' , 'Begin, Menachem' , 'Belloc, Hilaire' , 'Bellow, Saul' , 'Benchley, Robert' , 'Benenson, Peter' , 'Ben-Gurion, David' , 'Benjamin, Walter' , 'Benn, Tony' , 'Bennington, Chester' , 'Benson, Leana' , 'Bent, Silas' , 'Bentsen, Lloyd' , 'Berger, Ric' , 'Bergman, Ingmar' , 'Berio, Luciano' , 'Berle, Milton' , 'Berlin, Irving' , 'Berne, Eric' , 'Bernhard, Sandra' , 'Berra, Yogi' , 'Berry, Halle' , 'Berry, Wendell' , 'Bethea, Erin' , 'Bevan, Aneurin' , 'Bevel, Ken' , 'Biden, Joseph' , 'Bierce, Ambrose' , 'Biko, Steve' , 'Billings, Josh' , 'Biondo, Frank' , 'Birrell, Augustine' , 'Black, Elk' , 'Blair, Robert' , 'Blair, Tony' , 'Blake, William' ] ;
34+
35+ // Array.prototype.filter()
36+ // 1. Filter the list of inventors for those who were born in the 1500's
37+ const bornIn1500s = inventors . filter ( ( { year } ) => ( year >= 1500 && year < 1600 ) )
38+ console . group ( 'Born in 1500s' )
39+ console . table ( bornIn1500s )
40+ console . groupEnd ( )
41+
42+ // Array.prototype.map()
43+ // 2. Give us an array of the inventors' first and last names
44+ const fullNames = inventors . map ( ( { first, last } ) => `${ first } ${ last } ` )
45+ console . group ( 'Full Names' )
46+ console . table ( fullNames )
47+ console . groupEnd ( )
48+
49+ // Array.prototype.sort()
50+ // 3. Sort the inventors by birthdate, oldest to youngest
51+ const sorted = [ ...inventors ] . sort ( ( a , b ) => a . year - b . year )
52+ console . group ( 'Sorted by birthdate' )
53+ console . table ( sorted )
54+ console . groupEnd ( )
55+
56+ // Array.prototype.reduce()
57+ // 4. How many years did all the inventors live?
58+ const years = inventors . reduce ( ( sum , { year, passed } ) => sum + ( passed - year + 1 ) , 0 )
59+ console . group ( 'Total years lives' )
60+ console . log ( { years } )
61+ console . groupEnd ( )
62+
63+ // 5. Sort the inventors by years lived
64+ const lifespan = inventors . map ( person => {
65+ person . lifespan = ( person . passed - person . year + 1 )
66+ return person
67+ } ) . sort ( ( a , b ) => a . lifespan - b . lifespan )
68+ console . group ( 'Sorted by years lived' )
69+ console . table ( lifespan )
70+ console . groupEnd ( )
71+
72+ // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
73+ // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
74+
75+
76+ // 7. sort Exercise
77+ // Sort the people alphabetically by last name
78+ const alphabetically = [ ...inventors ] . sort ( ( a , b ) => a . last > b . last )
79+ console . group ( 'Sorted by Surname' )
80+ console . table ( alphabetically )
81+ console . groupEnd ( )
82+
83+ // 8. Reduce Exercise
84+ // Sum up the instances of each of these
85+ const data = [ 'car' , 'car' , 'truck' , 'truck' , 'bike' , 'walk' , 'car' , 'van' , 'bike' , 'walk' , 'car' , 'van' , 'car' , 'truck' ] ;
86+ const carStats = data . reduce ( ( tally , vehicle ) => {
87+ if ( ! tally . hasOwnProperty ( vehicle ) ) {
88+ tally [ vehicle ] = 1
89+ } else {
90+ tally [ vehicle ] ++
91+ }
92+ return tally
93+ } , { } )
94+ console . group ( 'Counting cars' )
95+ console . table ( carStats )
96+ console . groupEnd ( )
97+
98+
99+ </ script >
100+ </ body >
101+
102+ </ html >
0 commit comments