|
28 | 28 | // Array.prototype.filter() |
29 | 29 | // 1. Filter the list of inventors for those who were born in the 1500's |
30 | 30 |
|
| 31 | + console.log('>> 1'); |
| 32 | + const fifteen = inventors.filter(e => (e.year >= 1499 && e.year <= 1599)); |
| 33 | + console.table(fifteen); |
| 34 | + |
31 | 35 | // Array.prototype.map() |
32 | 36 | // 2. Give us an array of the inventory first and last names |
33 | 37 |
|
| 38 | + console.log('>> 2'); |
| 39 | + const nameFirsts = inventors.map(e => `${e.first} ${e.last}`); |
| 40 | + |
| 41 | + console.log(nameFirsts); |
| 42 | + |
34 | 43 | // Array.prototype.sort() |
35 | 44 | // 3. Sort the inventors by birthdate, oldest to youngest |
36 | 45 |
|
| 46 | + |
| 47 | + console.log('>> 3'); |
| 48 | + const sortBirthdate = inventors.sort((a, b) => a.year - b.year); |
| 49 | + |
| 50 | + console.table(sortBirthdate); |
| 51 | + |
37 | 52 | // Array.prototype.reduce() |
38 | 53 | // 4. How many years did all the inventors live? |
39 | 54 |
|
| 55 | + console.log('>> 4'); |
| 56 | + const yearsAllInventors = inventors.reduce(function(a, b){ |
| 57 | + |
| 58 | + const Byears = b.passed - b.year; |
| 59 | + return a+Byears; |
| 60 | + |
| 61 | + }, 0); |
| 62 | + console.log(yearsAllInventors); |
| 63 | + |
40 | 64 | // 5. Sort the inventors by years lived |
41 | 65 |
|
| 66 | + console.log('>> 5'); |
| 67 | + const oldestToYoungest = inventors.sort(function (a, b){ |
| 68 | + |
| 69 | + const Ayears = a.passed - a.year; |
| 70 | + const Byears = b.passed - b.year; |
| 71 | + |
| 72 | + return Byears > Ayears ? 1 : -1; |
| 73 | + |
| 74 | + }); |
| 75 | + |
| 76 | + console.table(oldestToYoungest); |
| 77 | + |
42 | 78 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
43 | 79 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
44 | 80 |
|
| 81 | + // const category = document.querySelector('.mw-category'); |
| 82 | + // const links = Array.from(category.querySelectorAll('a')); |
| 83 | + // const de = links |
| 84 | + // .map((link) => link.textContent) |
| 85 | + // .filter(streetName => streetName.includes('de')); |
| 86 | + |
| 87 | + |
45 | 88 |
|
46 | 89 | // 7. sort Exercise |
47 | 90 | // Sort the people alphabetically by last name |
| 91 | + console.log('>> 6'); |
| 92 | + const alphabeticallySort = people.sort((a, b) => { |
| 93 | + const [aLast, aFirst] = a.split(', '); |
| 94 | + const [bLast, bFirst] = b.split(', '); |
| 95 | + |
| 96 | + if(bLast < aLast){ |
| 97 | + return 1; |
| 98 | + } |
| 99 | + |
| 100 | + return -1; |
| 101 | + }); |
| 102 | + |
| 103 | + console.log(alphabeticallySort); |
48 | 104 |
|
49 | 105 | // 8. Reduce Exercise |
50 | 106 | // Sum up the instances of each of these |
| 107 | + |
| 108 | + console.log('>> 8'); |
51 | 109 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
52 | 110 |
|
| 111 | + const transportation = data.reduce(function(obj, item){ |
| 112 | + |
| 113 | + if(!obj[item]){ |
| 114 | + obj[item] = 0; |
| 115 | + } |
| 116 | + |
| 117 | + obj[item]++; |
| 118 | + |
| 119 | + return obj; |
| 120 | + |
| 121 | + }, {}) |
| 122 | + |
| 123 | + console.log(transportation) |
| 124 | + |
53 | 125 | </script> |
54 | 126 | </body> |
55 | 127 | </html> |
0 commit comments