|
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 | + let sixteenthCentury = inventors.filter(i => i.year > 1499 && i.year < 1600); |
| 35 | + // console.table(sixteenthCentury); |
34 | 36 |
|
35 | 37 | // Array.prototype.map() |
36 | 38 | // 2. Give us an array of the inventors' first and last names |
| 39 | + let firstLast = inventors.map(i => `${i.first} ${i.last}`); |
| 40 | + // console.log(firstLast); |
37 | 41 |
|
38 | 42 | // Array.prototype.sort() |
39 | 43 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 44 | + inventors.sort((a, b) => b.year - a.year); |
| 45 | + // console.log('Oldest to youngest'); |
| 46 | + // console.table(inventors); |
40 | 47 |
|
41 | 48 | // Array.prototype.reduce() |
42 | 49 | // 4. How many years did all the inventors live? |
| 50 | + let totalYears =inventors.reduce((total, i) => (total + (i.passed - i.year)), 0); |
| 51 | + // console.log('Total years', totalYears); |
| 52 | + |
| 53 | + // Wes' solution |
| 54 | + // const totalYears = inventors.reduce((total, inventor) => { |
| 55 | + // return total + (inventor.passed - inventor.year); |
| 56 | + // }, 0); |
| 57 | + // console.log('Total years:', totalYears); |
43 | 58 |
|
44 | 59 | // 5. Sort the inventors by years lived |
| 60 | + console.log('Sort by years lived'); |
| 61 | + inventors.forEach(i => i.age = (i.passed - i.year)); |
| 62 | + inventors.sort((a, b) => b.age - a.age); |
| 63 | + console.table(inventors); |
45 | 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 |
48 | 67 |
|
| 68 | + // Have to run this code against the Wikipedia page |
| 69 | + // let category = document.querySelector('.mw-category'); |
| 70 | + |
| 71 | + // // querySelectorAll returns a NodeList, not an array |
| 72 | + // let links = Array.from(category.querySelectorAll('a')); |
| 73 | + |
| 74 | + // let de = links |
| 75 | + // .map(link => link.textContent) |
| 76 | + // .filter(link => link.includes(' de ')); |
49 | 77 |
|
50 | 78 | // 7. sort Exercise |
51 | 79 | // Sort the people alphabetically by last name |
| 80 | + people.sort((lastOne, nextOne) => { |
| 81 | + let [aLast, aFirst] = lastOne.split(', '); |
| 82 | + let [bLast, bFirst] = nextOne.split(', '); |
| 83 | + |
| 84 | + if(aLast < bLast) return -1; |
| 85 | + else if (aLast === bLast) return 0; |
| 86 | + else return 1; |
| 87 | + }); |
| 88 | + // console.log(people); |
52 | 89 |
|
53 | 90 | // 8. Reduce Exercise |
54 | 91 | // Sum up the instances of each of these |
55 | 92 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
56 | 93 |
|
| 94 | + let transportation = data.reduce((obj, item) => { |
| 95 | + if(!obj[item]) { |
| 96 | + obj[item] = 0; |
| 97 | + } |
| 98 | + |
| 99 | + obj[item]++ |
| 100 | + return obj; |
| 101 | + }, {}); |
| 102 | + |
| 103 | + console.table(transportation); |
| 104 | + |
57 | 105 | </script> |
58 | 106 | </body> |
59 | 107 | </html> |
0 commit comments