Skip to content

Commit 37560e4

Browse files
committed
Finished video 4
1 parent b4beb6a commit 37560e4

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,62 @@
2727

2828
// Array.prototype.filter()
2929
// 1. Filter the list of inventors for those who were born in the 1500's
30+
console.table(inventors.filter(inventor => inventor.year > 1500 && inventor.year < 1600));
3031

3132
// Array.prototype.map()
3233
// 2. Give us an array of the inventory first and last names
34+
console.log(inventors.map(({first, last}) => `${first} ${last}`));
3335

3436
// Array.prototype.sort()
3537
// 3. Sort the inventors by birthdate, oldest to youngest
38+
console.table(inventors.sort((x, y) => x.year > y.year ? 1 : -1));
3639

3740
// Array.prototype.reduce()
3841
// 4. How many years did all the inventors live?
42+
console.log(inventors.reduce(((acc, {year, passed}) => acc += (passed - year)), 0));
3943

4044
// 5. Sort the inventors by years lived
45+
console.table(inventors.sort((a,b) => {
46+
const yearsA = a.passed - a.year;
47+
const yearsB = b.passed - b.year;
48+
return yearsA > yearsB ? -1 : 1;
49+
}));
4150

4251
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4352
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
53+
// const category = document.querySelector('.mw-category');
54+
// // const links = [...category.querySelectorAll('a')];
55+
// const links = Array.from(category.querySelectorAll('a'));
56+
// const de = links
57+
// .map((link) => link.textContent)
58+
// // .filter((text) => text.match(/de/));
59+
// .filter((text) => text.includes('de'));
60+
// console.log(de);
4461

4562

4663
// 7. sort Exercise
4764
// Sort the people alphabetically by last name
65+
const alpha = people.sort((a, b) => {
66+
const [aLast, aFirst] = a.split(', ');
67+
const [bLast, bFirst] = b.split(', ');
68+
return aLast > bLast ? 1 : -1;
69+
});
70+
console.log(alpha);
71+
// this doesn't return the whole name:
72+
// const lastNames = people.map((name) => name.split(',')[0]);
73+
// console.log(lastNames.sort());
4874

4975
// 8. Reduce Exercise
5076
// Sum up the instances of each of these
5177
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
52-
78+
const dataSum = data.reduce((acc, item) => {
79+
if (!acc[item]) {
80+
acc[item] = 0;
81+
}
82+
acc[item]++;
83+
return acc;
84+
}, {});
85+
console.log(dataSum);
5386
</script>
5487
</body>
5588
</html>

0 commit comments

Comments
 (0)