Visitar URL original
complete array cardio day 1 · wesbos/JavaScript30@60cab87 · GitHub
Skip to content

Commit 60cab87

Browse files
committed
complete array cardio day 1
1 parent 53af5c2 commit 60cab87

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,65 @@
3333

3434
// Array.prototype.filter()
3535
// 1. Filter the list of inventors for those who were born in the 1500's
36+
const bornIn50s = inventors.filter(inventor => inventor.year >= 1500 && inventor.year < 1600);
37+
console.table(bornIn50s);
3638

3739
// Array.prototype.map()
3840
// 2. Give us an array of the inventors' first and last names
41+
const names = inventors.map(inventor => `${inventor.first} ${inventor.last}`);
42+
console.log(names);
3943

4044
// Array.prototype.sort()
4145
// 3. Sort the inventors by birthdate, oldest to youngest
46+
const sortedInventors = inventors.sort((a, b) => a.year > b.year ? 1 : -1);
47+
console.table(sortedInventors);
4248

4349
// Array.prototype.reduce()
4450
// 4. How many years did all the inventors live?
51+
const totalYears = inventors.reduce((total, inventor) => {
52+
return total + (inventor.passed - inventor.year);
53+
}, 0);
54+
console.log(totalYears);
4555

4656
// 5. Sort the inventors by years lived
57+
const sortedOldest = inventors.sort((inventor1, inventor2) => {
58+
const inventor1Age = inventor1.passed - inventor1.year;
59+
const inventor2Age = inventor2.passed - inventor2.year;
60+
return inventor1Age > inventor2Age ? -1 : 1;
61+
});
62+
console.table(sortedOldest);
4763

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

67+
// const category = document.querySelector('.mw-category');
68+
// const links = Array.from(category.querySelectorAll('a'));
69+
//
70+
// const de = links
71+
// .map(link => link.textContent)
72+
// .filter(boulevard => boulevard.includes('de'));
5173

5274
// 7. sort Exercise
5375
// Sort the people alphabetically by last name
76+
const alpha = people.sort((lastOne, nextOne) => {
77+
const [aLast, aFirst] = lastOne.split(', ');
78+
const [bLast, bFirst] = nextOne.split(', ');
79+
return aLast > bLast ? 1 : -1;
80+
});
81+
console.log(alpha);
5482

5583
// 8. Reduce Exercise
5684
// Sum up the instances of each of these
5785
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
86+
const transporation = data.reduce((obj, item) => {
87+
if (!obj[item]) {
88+
obj[item] = 1;
89+
} else {
90+
obj[item]++;
91+
}
92+
return obj;
93+
}, {});
94+
console.log(transporation);
5895

5996
</script>
6097
</body>

0 commit comments

Comments
 (0)