Skip to content

Commit ec1f805

Browse files
committed
Finished day wesbos#4: Array Cardio Day 1
1 parent 933006d commit ec1f805

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

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

+39
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,68 @@
3333

3434
// Array.prototype.filter()
3535
// 1. Filter the list of inventors for those who were born in the 1500's
36+
const fifteen = inventors.filter(function (item) {
37+
if(item.year >= 1500 && item.year <= 1599) {
38+
return true;
39+
}
40+
});
41+
console.table(fifteen);
3642

3743
// Array.prototype.map()
3844
// 2. Give us an array of the inventors' first and last names
45+
const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`);
46+
console.log(fullNames);
3947

4048
// Array.prototype.sort()
4149
// 3. Sort the inventors by birthdate, oldest to youngest
50+
const ordered = inventors.sort((a, b) => (a.year > b.year) ? 1 : -1);
51+
console.table(ordered);
4252

4353
// Array.prototype.reduce()
4454
// 4. How many years did all the inventors live?
55+
const totalYears = inventors.reduce((total, inventor) => {
56+
return total + (inventor.passed - inventor.year);
57+
}, 0);
58+
console.log(totalYears);
4559

4660
// 5. Sort the inventors by years lived
61+
const sorted = inventors.sort(function(a, b) {
62+
const years = (inventor) => inventor.passed - inventor.year;
63+
return (years(a) > years(b)) ? -1 : 1;
64+
});
65+
console.table(sorted);
4766

4867
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4968
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
5069

70+
// const category = document.querySelector('.mw-category');
71+
// const links = Array.from(category.querySelectorAll('a')); // because there is NodeList
72+
// const de = links
73+
// .map(link => link.textContent)
74+
// .filter(streetName => streetName.includes('de'));
5175

5276
// 7. sort Exercise
5377
// Sort the people alphabetically by last name
78+
const alpha = people.sort((a, b) => {
79+
const [aLast, aFirst] = a.split(', ');
80+
const [bLast, bFirst] = b.split(', ');
81+
return aLast > bLast ? 1 : -1;
82+
});
83+
console.log(alpha);
5484

5585
// 8. Reduce Exercise
5686
// Sum up the instances of each of these
5787
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
5888

89+
const transport = data.reduce(function(obj, item) {
90+
if(!obj[item]) {
91+
obj[item] = 0;
92+
}
93+
obj[item]++;
94+
return obj;
95+
}, {});
96+
console.log(transport);
97+
5998
</script>
6099
</body>
61100
</html>

0 commit comments

Comments
 (0)