|
32 | 32 |
|
33 | 33 | // Array.prototype.filter()
|
34 | 34 | // 1. Filter the list of inventors for those who were born in the 1500's
|
| 35 | + console.log(inventors.filter(function(v) { |
| 36 | + return v.year > 1499 && v.year < 1600; |
| 37 | + })); |
35 | 38 |
|
36 | 39 | // Array.prototype.map()
|
37 | 40 | // 2. Give us an array of the inventors' first and last names
|
| 41 | + console.log( |
| 42 | + inventors.map(function(v){ |
| 43 | + return `${v.first} ${v.last}`; |
| 44 | + }) |
| 45 | + ); |
| 46 | + |
| 47 | + //Age of each inventor |
| 48 | + console.table( |
| 49 | + inventors.map(function(v){ |
| 50 | + v.age = v.passed-v.year; |
| 51 | + return v; |
| 52 | + }) |
| 53 | + ); |
38 | 54 |
|
39 | 55 | // Array.prototype.sort()
|
40 | 56 | // 3. Sort the inventors by birthdate, oldest to youngest
|
41 |
| - |
| 57 | + var sorted = inventors.sort((a,b) => a.year - b.year); |
| 58 | + console.table(sorted); |
| 59 | + |
42 | 60 | // Array.prototype.reduce()
|
43 | 61 | // 4. How many years did all the inventors live?
|
| 62 | + var totalAges = inventors.reduce(function(a,b){ |
| 63 | + a += b.passed-b.year; |
| 64 | + return a; |
| 65 | + }, 0) |
| 66 | + console.log(totalAges); |
44 | 67 |
|
45 | 68 | // 5. Sort the inventors by years lived
|
| 69 | + sorted = inventors.sort((a,b) => (a.age - b.age)); |
| 70 | + console.table(sorted); |
46 | 71 |
|
47 | 72 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
|
48 | 73 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
|
49 |
| - |
| 74 | + // const wholeList = document.querySelector('.mw-category'); |
| 75 | + // const links = [...wholeList.querySelectorAll('a')]; |
| 76 | + // // const links = Array.from(wholeList.querySelectorAll('a')); |
| 77 | + // const deStreets = links |
| 78 | + // .map(item => item.textContent) |
| 79 | + // .filter(item => item.includes('de')); |
| 80 | + // console.log(deStreets); |
50 | 81 |
|
51 | 82 | // 7. sort Exercise
|
52 | 83 | // Sort the people alphabetically by last name
|
| 84 | + const sortedPeople = people.sort(function(current, next){ |
| 85 | + //if sort returns a negative, then current will be before next |
| 86 | + //if it returns a positive, then next will be before current |
| 87 | + //if zero, then they won't move relative to each other |
| 88 | + var thisLast = current.split(', ')[0]; |
| 89 | + var nextLast = next.split(', ')[0]; |
| 90 | + return thisLast < nextLast ? -1 : 1; |
| 91 | + }); |
| 92 | + console.log(sortedPeople); |
53 | 93 |
|
54 | 94 | // 8. Reduce Exercise
|
55 | 95 | // Sum up the instances of each of these
|
56 | 96 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
|
| 97 | + const instances = data.reduce(function(allVehicles, currentVehicle){ |
| 98 | + if (!(currentVehicle in allVehicles)) { |
| 99 | + allVehicles[currentVehicle] = 0; //initialize that property |
| 100 | + } |
| 101 | + allVehicles[currentVehicle]++; |
| 102 | + return allVehicles; |
| 103 | + },{}); |
| 104 | + console.log(instances); |
57 | 105 |
|
58 | 106 | </script>
|
59 | 107 | </body>
|
|
0 commit comments