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