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