|
35 | 35 | // Array.prototype.map() |
36 | 36 | // 2. Give us an array of the inventors first and last names |
37 | 37 | const inventornames = inventors.map(element => `${element.first} ${element.last}`); |
38 | | - console.log(inventornames); |
| 38 | + const tryMaps = inventors.map(function(element,index,array){ |
| 39 | + console.log(element); |
| 40 | + console.log(index); |
| 41 | + console.log(array); |
| 42 | + }) |
| 43 | + // console.log(inventornames); |
39 | 44 |
|
40 | 45 | // Array.prototype.sort() |
41 | 46 | // 3. Sort the inventors by birthdate, oldest to youngest |
42 | 47 | const inventorAges = inventors.sort((a,b) => (a.year - b.year)); |
43 | | - console.log('ages'); |
44 | | - console.log(inventorAges); |
| 48 | + // console.log('ages'); |
| 49 | + // console.log(inventorAges); |
45 | 50 |
|
46 | 51 | // Array.prototype.reduce() |
47 | 52 | // 4. How many years did all the inventors live all together? |
48 | 53 | const ageCombined_func = (accumulator, currentValue) => accumulator + (currentValue.passed-currentValue.year); |
49 | 54 | const ageCombined = inventors.reduce(ageCombined_func,0); |
50 | | - console.log('ages combined'); |
51 | | - console.log(ageCombined); |
| 55 | + //console.log('ages combined'); |
| 56 | + //console.log(ageCombined); |
52 | 57 |
|
53 | 58 | // 5. Sort the inventors by years lived |
54 | 59 | const inventorOld = inventors.sort((a,b) => ((a.year-a.passed) - (b.year-b.passed))); |
55 | | - console.log('old'); |
56 | | - console.log(inventorOld); |
| 60 | + //console.log('old'); |
| 61 | + //console.log(inventorOld); |
57 | 62 |
|
58 | 63 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
59 | 64 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
60 | 65 | let getAllAnchors = document.querySelectorAll('.mw-category-group > ul > li > a'); |
61 | 66 | let allNames =[]; |
62 | 67 | getAllAnchors.forEach((a) => allNames.push((a.title))); |
63 | 68 | let allFilteredNames = allNames.filter(element => element.includes('de')); |
64 | | - console.log(allFilteredNames); |
| 69 | + //console.log(allFilteredNames); |
65 | 70 |
|
66 | 71 | // 7. sort Exercise |
67 | 72 | // Sort the people alphabetically by last name |
|
75 | 80 | // a must be equal to b |
76 | 81 | return 0; |
77 | 82 | }); |
78 | | - console.log('names'); |
79 | | - console.log(peopleName); |
| 83 | + // console.log('names'); |
| 84 | + // console.log(peopleName); |
80 | 85 |
|
81 | 86 | // 8. Reduce Exercise |
82 | 87 | // Sum up the instances of each of these |
83 | 88 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
84 | | - // const result = []; |
85 | | - // const instance_func = function(accumulator, currentValue){ |
86 | | - // if(data.filter((element) => element === currentValue)) |
87 | | - // }; |
88 | | - // const ageCombined = inventors.reduce(ageCombined_func,0); |
89 | | - // console.log('ages combined'); |
90 | | - // console.log(ageCombined); |
| 89 | + const transportation = data.reduce(function(obj, item) { |
| 90 | + if (!obj[item]) { |
| 91 | + obj[item] = 0; |
| 92 | + } |
| 93 | + obj[item]++; |
| 94 | + return obj; |
| 95 | + }, {}); |
91 | 96 |
|
92 | 97 | </script> |
93 | 98 | <script src="CustomHigherOrder.js"></script> |
|
0 commit comments