|
27 | 27 |
|
28 | 28 | // Array.prototype.filter()
|
29 | 29 | // 1. Filter the list of inventors for those who were born in the 1500's
|
| 30 | + let inventorsOf1500 = inventors.filter((inventor) => (inventor.year < 1600 && inventor.year >= 1500)); |
| 31 | + console.table(inventorsOf1500); |
30 | 32 |
|
31 | 33 | // Array.prototype.map()
|
32 | 34 | // 2. Give us an array of the inventory first and last names
|
| 35 | + let names = inventors.map((inventor) => inventor.first + ' ' + inventor.last); |
| 36 | + console.log(names); |
33 | 37 |
|
34 | 38 | // Array.prototype.sort()
|
35 | 39 | // 3. Sort the inventors by birthdate, oldest to youngest
|
| 40 | + let inventorsSortByBirthday = inventors.sort((inventor1, inventor2) => inventor1.year - inventor2.year); |
| 41 | + console.table(inventorsSortByBirthday); |
| 42 | + |
36 | 43 |
|
37 | 44 | // Array.prototype.reduce()
|
38 | 45 | // 4. How many years did all the inventors live?
|
| 46 | + let yearsOfAllInventors = inventors.reduce((i, inventor) => i + (inventor.passed - inventor.year), 0); |
| 47 | + console.log(yearsOfAllInventors); |
39 | 48 |
|
40 |
| - // 5. Sort the inventors by years lived |
| 49 | + // 5. Sort the inventors by years lived highest at the beginning |
| 50 | + let inventorsByYearsLived = inventors.sort((inventor1, inventor2) => (inventor2.passed - inventor2.year) - (inventor1.passed - inventor1.year)); |
| 51 | + console.table(inventorsByYearsLived); |
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
|
| 55 | + //let category = document.querySelector('.mw-category'); |
| 56 | + //let links = [...category.querySelectorAll('a')].map(a => a.textContent); |
| 57 | + //let de = links.filter(rue => rue.includes('de')); |
| 58 | + //console.log(de); |
44 | 59 |
|
45 | 60 |
|
46 | 61 | // 7. sort Exercise
|
47 | 62 | // Sort the people alphabetically by last name
|
| 63 | + let alphabetical = people.sort((current, next) => { |
| 64 | + let [currentLast, currentFirst] = current.split(', '); |
| 65 | + let [nextLast, nextFirst] = next.split(', '); |
| 66 | + return currentLast < nextLast ? -1 : 1; |
| 67 | + }); |
| 68 | + console.log(alphabetical); |
48 | 69 |
|
49 | 70 | // 8. Reduce Exercise
|
50 | 71 | // Sum up the instances of each of these
|
51 | 72 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
|
| 73 | + let numberOfAppearance = data.reduce((appearance, item) => { |
| 74 | + if(!appearance[item]) { |
| 75 | + appearance[item] = 0; |
| 76 | + } |
| 77 | + appearance[item]++; |
| 78 | + return appearance; |
| 79 | + }, {}); |
| 80 | + console.log(numberOfAppearance); |
52 | 81 |
|
53 | 82 | </script>
|
54 | 83 | </body>
|
|
0 commit comments