Skip to content

Commit a0e78d2

Browse files
Day 4
1 parent 0f23062 commit a0e78d2

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,60 @@
3333

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

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

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

4349
// Array.prototype.reduce()
4450
// 4. How many years did all the inventors live?
51+
const totalYearsOfScience = inventors.reduce((sum, inventor) => sum + lived(inventor), 0)
52+
console.log(totalYearsOfScience);
4553

4654
// 5. Sort the inventors by years lived
55+
const oldest = [].concat(inventors).sort((a, b) => lived(b) - lived(a));
56+
console.table(oldest);
4757

4858
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4959
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
50-
60+
const boulevardsWithDe = Array.prototype.map.call(
61+
document.querySelectorAll('.mw-category li a'),
62+
anchor => anchor.textContent
63+
).filter(boulevard => boulevard.indexOf('de') !== -1);
5164

5265
// 7. sort Exercise
5366
// Sort the people alphabetically by last name
67+
const alphabeticallyOrdered = [].concat(people).sort((a, b) => lastName(a) > lastName(b) ? 1 : -1);
68+
console.log(alphabeticallyOrdered);
5469

5570
// 8. Reduce Exercise
5671
// Sum up the instances of each of these
5772
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
73+
const countsOfVehicles = data.reduce(
74+
(hash, vehicle) => {
75+
hash[vehicle] = (hash[vehicle] || 0) + 1;
76+
return hash;
77+
},
78+
{}
79+
);
80+
console.log(countsOfVehicles);
81+
82+
function lived(inventor) {
83+
return inventor.passed - inventor.year;
84+
}
5885

86+
function lastName(fullName) {
87+
const [_, last] = fullName.split(', ');
88+
return last;
89+
}
5990
</script>
6091
</body>
6192
</html>

0 commit comments

Comments
 (0)