|
34 | 34 | 'Berne, Eric', 'Berra, Yogi', 'Berry, Wendell', 'Bevan, Aneurin', 'Ben-Gurion, David', 'Bevel, Ken', 'Biden, Joseph', 'Bennington, Chester', 'Bierce, Ambrose',
|
35 | 35 | 'Billings, Josh', 'Birrell, Augustine', 'Blair, Tony', 'Beecher, Henry', 'Biondo, Frank'
|
36 | 36 | ];
|
37 |
| - |
| 37 | + |
38 | 38 | // Array.prototype.filter()
|
39 | 39 | // 1. Filter the list of inventors for those who were born in the 1500's
|
| 40 | + const fifteens = inventors.filter(person => (person.year >= 1500 && person.year <1600)); |
| 41 | + console.table(fifteens); |
40 | 42 |
|
41 | 43 | // Array.prototype.map()
|
42 | 44 | // 2. Give us an array of the inventors first and last names
|
| 45 | + const names = inventors.map(person => person.first + ' ' + person.last); |
| 46 | + console.log(names); |
43 | 47 |
|
44 | 48 | // Array.prototype.sort()
|
45 | 49 | // 3. Sort the inventors by birthdate, oldest to youngest
|
| 50 | + const sorted = inventors.sort((a, b) => a.year - b.year); |
| 51 | + console.table(sorted); |
46 | 52 |
|
47 | 53 | // Array.prototype.reduce()
|
48 | 54 | // 4. How many years did all the inventors live all together?
|
| 55 | + const totalYears = inventors.reduce((total, person) => total += (person.passed - person.year), 0); |
| 56 | + console.log(totalYears); |
49 | 57 |
|
50 | 58 | // 5. Sort the inventors by years lived
|
| 59 | + const youngToOld = inventors.sort((a, b) => ((a.passed - a.year) - (b.passed - b.year))); |
| 60 | + console.table(youngToOld); |
51 | 61 |
|
52 | 62 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
|
53 | 63 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
|
| 64 | + // do this one on the web page |
| 65 | + |
| 66 | + // const category = document.querySelector('.mw-catagory'); |
| 67 | + // const links = Array.from(category.querySelectorAll('a')); |
| 68 | + // const de = links.map(link => link.textContent) |
| 69 | + // .filter(streetName => streetName.includes('de')); |
54 | 70 |
|
55 | 71 |
|
56 | 72 | // 7. sort Exercise
|
57 | 73 | // Sort the people alphabetically by last name
|
| 74 | + const peopleSorted = people.sort((a, b) => { |
| 75 | + const aName = a.split(', '); |
| 76 | + const bName = b.split(', '); |
| 77 | + return (aName[0] > bName[0]) ? 1 : -1; |
| 78 | + }); |
| 79 | + console.log(peopleSorted); |
58 | 80 |
|
59 | 81 | // 8. Reduce Exercise
|
60 | 82 | // Sum up the instances of each of these
|
61 | 83 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
|
62 | 84 |
|
| 85 | + const instances = data.reduce((obj, item) => { |
| 86 | + (obj[item]) ? obj[item]++ : obj[item] = 1; |
| 87 | + return obj |
| 88 | + }, {}); |
| 89 | + console.log(instances); |
| 90 | + |
63 | 91 | </script>
|
64 | 92 | </body>
|
65 | 93 | </html>
|
0 commit comments