Skip to content

Commit 8ee5183

Browse files
author
Jess Hines
committed
JDH: Finish wesbos#4
1 parent c7568cd commit 8ee5183

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

04 - Array Cardio Day 1/index-START.html

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,76 @@
3232

3333
// Array.prototype.filter()
3434
// 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+
}));
3538

3639
// Array.prototype.map()
3740
// 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+
);
3854

3955
// Array.prototype.sort()
4056
// 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+
4260
// Array.prototype.reduce()
4361
// 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);
4467

4568
// 5. Sort the inventors by years lived
69+
sorted = inventors.sort((a,b) => (a.age - b.age));
70+
console.table(sorted);
4671

4772
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4873
// 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);
5081

5182
// 7. sort Exercise
5283
// 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);
5393

5494
// 8. Reduce Exercise
5595
// Sum up the instances of each of these
5696
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);
57105

58106
</script>
59107
</body>

0 commit comments

Comments
 (0)