|
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 | + var filtered = inventors.filter(inventor => inventor.year > 1499 && inventor.year < 1600); |
40 | 41 |
|
41 | 42 | // Array.prototype.map() |
42 | 43 | // 2. Give us an array of the inventors first and last names |
| 44 | + var names = inventors.map(inventor => inventor.first + ' ' + inventor.last); |
43 | 45 |
|
44 | 46 | // Array.prototype.sort() |
45 | 47 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 48 | + var sorted = inventors.sort((inventor, next) => inventor.year > next.year ? 1 : -1); |
46 | 49 |
|
47 | 50 | // Array.prototype.reduce() |
48 | 51 | // 4. How many years did all the inventors live all together? |
| 52 | + var yearsLived = inventors.reduce(function(yrs, inventor) { |
| 53 | + return yrs += inventor.passed - inventor.year; |
| 54 | + }, 0); |
49 | 55 |
|
50 | 56 | // 5. Sort the inventors by years lived |
| 57 | + var oldest = inventors.sort((inventor, next) => inventor.passed - inventor.year < next.passed - next.year ? 1 : -1); |
51 | 58 |
|
52 | 59 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
53 | 60 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
54 | | - |
| 61 | + // const category = document.querySelector('.mw-category'); |
| 62 | + // const links = Array.from(category.querySelectorAll('a')); |
| 63 | + // const de = links |
| 64 | + // .map(link => link.textContent) |
| 65 | + // .filter(streetName => streetName.includes('de')); |
55 | 66 |
|
56 | 67 | // 7. sort Exercise |
57 | 68 | // Sort the people alphabetically by last name |
58 | 69 |
|
| 70 | + // My Solution |
| 71 | + var sortedLastNames = people.sort((person, nextPerson) => person.split(', ')[0] > nextPerson.split(', ')[0] ? 1 : -1); |
| 72 | + |
| 73 | + // Wes Solution |
| 74 | + var alpha = people.sort((lastOne, nextOne) => { |
| 75 | + var [aLast, aFirst] = lastOne.split(', ')[0]; |
| 76 | + var [bLast, bFirst] = nextOne.split(', ')[0]; |
| 77 | + aLast > bLast ? 1 : -1; |
| 78 | + }); |
| 79 | + |
59 | 80 | // 8. Reduce Exercise |
60 | 81 | // Sum up the instances of each of these |
61 | 82 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
62 | 83 |
|
| 84 | + var transportation = data.reduce(function(acc, item) { |
| 85 | + acc[item] = acc[item] + 1 || 1; |
| 86 | + return acc; |
| 87 | + }, {}); |
63 | 88 | </script> |
64 | 89 | </body> |
65 | 90 | </html> |
0 commit comments