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