|
32 | 32 | // Array.prototype.filter() |
33 | 33 | // 1. Filter the list of inventors for those who were born in the 1500's |
34 | 34 |
|
| 35 | + const result = inventors.filter((elem) => elem.year >= 1500 && elem.year < 1600); |
| 36 | + console.table(result); |
| 37 | + |
35 | 38 | // Array.prototype.map() |
36 | 39 | // 2. Give us an array of the inventors' first and last names |
37 | 40 |
|
| 41 | + const names = inventors.map((elem) => elem.first + ' ' + elem.last); |
| 42 | + console.table(names); |
| 43 | + |
38 | 44 | // Array.prototype.sort() |
39 | 45 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 46 | + inventors.sort((a, b) => (a.year > b.year) ? 1 : -1); |
| 47 | + console.table(inventors); |
40 | 48 |
|
41 | 49 | // Array.prototype.reduce() |
42 | 50 | // 4. How many years did all the inventors live? |
43 | 51 |
|
| 52 | + const totalYears = inventors.reduce(((sum, currentValue) => { |
| 53 | + return sum + currentValue.passed - currentValue.year; |
| 54 | + }), 0); |
| 55 | + console.log(totalYears); |
| 56 | + |
44 | 57 | // 5. Sort the inventors by years lived |
45 | 58 |
|
| 59 | + const yearsLived = inventors.sort((a, b) => (a.passed - a.year < b.passed - b.year ? 1 : -1)); |
| 60 | + console.table(yearsLived); |
| 61 | + |
46 | 62 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
47 | 63 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
48 | 64 |
|
| 65 | + // const category = document.querySelector('.mw-category'); |
| 66 | + // const links = category.querySelectorAll('a'); |
| 67 | + // let res = []; |
| 68 | + // links.forEach((item) => { |
| 69 | + // // console.log(typeof item.title) |
| 70 | + // if (/de/i.test(item.title)) { |
| 71 | + // res.push(item.title); |
| 72 | + // } |
| 73 | + // }); |
| 74 | + // console.log(res); |
| 75 | + // // console.log(links); |
| 76 | + |
49 | 77 |
|
50 | 78 | // 7. sort Exercise |
51 | 79 | // Sort the people alphabetically by last name |
| 80 | + lastNameSorted = people.sort((a, b) => { |
| 81 | + const surnameA = a.split(','); |
| 82 | + const surnameB = b.split(','); |
| 83 | + return surnameA > surnameB ? 1 : -1; |
| 84 | + }); |
| 85 | + console.table(lastNameSorted); |
| 86 | + |
52 | 87 |
|
53 | 88 | // 8. Reduce Exercise |
54 | 89 | // Sum up the instances of each of these |
55 | 90 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
56 | 91 |
|
| 92 | + const dataRes = data.reduce(function(obj, item) { |
| 93 | + // console.log(item); |
| 94 | + if (!obj[item]) { |
| 95 | + obj[item] = 0; |
| 96 | + } |
| 97 | + obj[item]++; |
| 98 | + return obj; |
| 99 | + |
| 100 | + }, {}); |
| 101 | + console.log(dataRes); |
| 102 | + |
| 103 | + |
| 104 | + |
57 | 105 | </script> |
58 | 106 | </body> |
59 | 107 | </html> |
0 commit comments