|
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 | + console.table(inventors.filter(inventor => inventor.year > 1500 && inventor.year < 1600)); |
30 | 31 |
|
31 | 32 | // Array.prototype.map() |
32 | 33 | // 2. Give us an array of the inventory first and last names |
| 34 | + console.log(inventors.map(({first, last}) => `${first} ${last}`)); |
33 | 35 |
|
34 | 36 | // Array.prototype.sort() |
35 | 37 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 38 | + console.table(inventors.sort((x, y) => x.year > y.year ? 1 : -1)); |
36 | 39 |
|
37 | 40 | // Array.prototype.reduce() |
38 | 41 | // 4. How many years did all the inventors live? |
| 42 | + console.log(inventors.reduce(((acc, {year, passed}) => acc += (passed - year)), 0)); |
39 | 43 |
|
40 | 44 | // 5. Sort the inventors by years lived |
| 45 | + console.table(inventors.sort((a,b) => { |
| 46 | + const yearsA = a.passed - a.year; |
| 47 | + const yearsB = b.passed - b.year; |
| 48 | + return yearsA > yearsB ? -1 : 1; |
| 49 | + })); |
41 | 50 |
|
42 | 51 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
43 | 52 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 53 | + // const category = document.querySelector('.mw-category'); |
| 54 | + // // const links = [...category.querySelectorAll('a')]; |
| 55 | + // const links = Array.from(category.querySelectorAll('a')); |
| 56 | + // const de = links |
| 57 | + // .map((link) => link.textContent) |
| 58 | + // // .filter((text) => text.match(/de/)); |
| 59 | + // .filter((text) => text.includes('de')); |
| 60 | + // console.log(de); |
44 | 61 |
|
45 | 62 |
|
46 | 63 | // 7. sort Exercise |
47 | 64 | // Sort the people alphabetically by last name |
| 65 | + const alpha = people.sort((a, b) => { |
| 66 | + const [aLast, aFirst] = a.split(', '); |
| 67 | + const [bLast, bFirst] = b.split(', '); |
| 68 | + return aLast > bLast ? 1 : -1; |
| 69 | + }); |
| 70 | + console.log(alpha); |
| 71 | + // this doesn't return the whole name: |
| 72 | + // const lastNames = people.map((name) => name.split(',')[0]); |
| 73 | + // console.log(lastNames.sort()); |
48 | 74 |
|
49 | 75 | // 8. Reduce Exercise |
50 | 76 | // Sum up the instances of each of these |
51 | 77 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
52 | | - |
| 78 | + const dataSum = data.reduce((acc, item) => { |
| 79 | + if (!acc[item]) { |
| 80 | + acc[item] = 0; |
| 81 | + } |
| 82 | + acc[item]++; |
| 83 | + return acc; |
| 84 | + }, {}); |
| 85 | + console.log(dataSum); |
53 | 86 | </script> |
54 | 87 | </body> |
55 | 88 | </html> |
0 commit comments