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