|
66 | 66 | console.log('totalyears: ' + totalyears) |
67 | 67 |
|
68 | 68 | // 5. Sort the inventors by years lived |
69 | | - const inventorsPlusAges = inventors |
70 | | - //console.log(inventors[0]) |
71 | | - i = 0 |
72 | | - inventors.forEach(function(inventor){ |
73 | | - //console.log(inventor) |
74 | | - //console.log(i) |
75 | | - //console.log(ages[i]) |
76 | | - //inventorsPlusAges[i].concat(ages[i]) |
77 | | - i+=1 |
| 69 | + console.log('Sort the inventors by years lived: ') |
| 70 | + // const inventorsPlusAges = inventors |
| 71 | + // //console.log(inventors[0]) |
| 72 | + // i = 0 |
| 73 | + // inventors.forEach(function(inventor){ |
| 74 | + // //console.log(inventor) |
| 75 | + // //console.log(i) |
| 76 | + // //console.log(ages[i]) |
| 77 | + // //inventorsPlusAges[i].concat(ages[i]) |
| 78 | + // i+=1 |
| 79 | + // }) |
| 80 | + //console.table(inventorsPlusAges) |
| 81 | + |
| 82 | + const byYearsLived = inventors.sort(function(a,b){ |
| 83 | + return ((a.year-a.passed) - (b.year-b.passed)) |
78 | 84 | }) |
79 | | - console.table(inventorsPlusAges) |
| 85 | + console.table(byYearsLived.reverse()) |
| 86 | + |
80 | 87 |
|
81 | 88 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
82 | 89 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 90 | + //a = document.location.load="https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris" |
| 91 | + //a = load('https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris') |
| 92 | + //console.table(a) |
| 93 | + //a = fetch('https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris') |
83 | 94 |
|
84 | 95 |
|
85 | 96 | // 7. sort Exercise |
86 | 97 | // Sort the people alphabetically by last name |
| 98 | + alphaName = people.sort(function(a,b){ |
| 99 | + return((a[0]-b[0])) |
| 100 | + }) |
| 101 | + console.table(alphaName) |
87 | 102 |
|
88 | 103 | // 8. Reduce Exercise |
89 | 104 | // Sum up the instances of each of these |
90 | | - const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
91 | | - //const ages = inventors.map(inventor => inventor.passed-inventor.year) |
92 | | - //const reducer = (accumulator, currentValue) => accumulator + currentValue |
| 105 | + const dataOrig = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 106 | + var data = dataOrig.reduce(function(itemCount, currentItem){ |
| 107 | + if(typeof itemCount[currentItem] !== "undefined"){ |
| 108 | + itemCount[currentItem]++; |
| 109 | + return itemCount; |
| 110 | + } else { |
| 111 | + itemCount[currentItem]=1; |
| 112 | + return itemCount; |
| 113 | + } |
| 114 | + }, {}); |
| 115 | + var numItems = []; |
| 116 | + for(var x in data){ |
| 117 | + numItems.push(x + ": " + data[x]); |
| 118 | + } |
| 119 | + console.log(numItems); |
93 | 120 |
|
94 | 121 | </script> |
95 | 122 | </body> |
|
0 commit comments