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