11<!DOCTYPE html>
22< html lang ="en ">
3+
34< head >
45 < meta charset ="UTF-8 ">
56 < title > Array Cardio 💪</ title >
67</ head >
8+
79< body >
810 < p > < em > Psst: have a look at the JavaScript Console</ em > 💁</ p >
911 < script >
3234 // Array.prototype.filter()
3335 // 1. Filter the list of inventors for those who were born in the 1500's
3436
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+
3541 // Array.prototype.map()
3642 // 2. Give us an array of the inventors' first and last names
3743
44+ const mapList = inventors . map ( i => i . first + ' ' + i . last ) ;
45+ console . log ( 'Inventors names : ' ) ;
46+ console . log ( mapList ) ;
47+
48+
3849 // Array.prototype.sort()
3950 // 3. Sort the inventors by birthdate, oldest to youngest
4051
52+ const sortList = inventors . sort ( ( a , b ) => a . year > b . year ? 1 : - 1 ) ;
53+ console . log ( 'Inventors birthdates : ' ) ;
54+ console . table ( sortList ) ;
55+
4156 // Array.prototype.reduce()
4257 // 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 ) ;
4363
4464 // 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 ) ;
4569
4670 // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4771 // 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+ */
4980
5081 // 7. sort Exercise
5182 // 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+
5288
5389 // 8. Reduce Exercise
5490 // 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' ] ;
5692
93+ const reducedData = data . reduce ( ( obj , count ) => {
94+ obj [ count ] ? obj [ count ] ++ : obj [ count ] = 1 ;
95+ return obj ;
96+ } , { } ) ;
97+
98+ console . log ( reducedData ) ;
5799 </ script >
58100</ body >
59- </ html >
101+
102+ </ html >
0 commit comments