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