Skip to content

Commit 0c48c69

Browse files
committed
Day 4 done
1 parent 9e09898 commit 0c48c69

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,73 @@
3131

3232
// Array.prototype.filter()
3333
// 1. Filter the list of inventors for those who were born in the 1500's
34+
const fifteen = inventors.filter(function(i){
35+
return i.year >= 1500 && i.year < 1600;
36+
});
37+
38+
// Can be written as
39+
//const fifteen = inventors.filter(i => i.year >= 1500 && i.year < 1600);
40+
console.table(fifteen);
3441

3542
// Array.prototype.map()
3643
// 2. Give us an array of the inventors' first and last names
44+
const fullNames = inventors.map(i => `${i.first} ${i.last}`);
45+
console.log(fullNames);
3746

3847
// Array.prototype.sort()
3948
// 3. Sort the inventors by birthdate, oldest to youngest
49+
// const ordered = inventors.sort(function(a, b){
50+
// return a.year - b.year;
51+
// });
52+
53+
const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1);
54+
console.table(ordered);
4055

4156
// Array.prototype.reduce()
4257
// 4. How many years did all the inventors live?
58+
const totalYears = inventors.reduce((total, inventor) => {
59+
return total + (inventor.passed - inventor.year);
60+
}, 0);
61+
console.log(totalYears);
4362

4463
// 5. Sort the inventors by years lived
64+
const sortedByYearsLived = inventors.sort(function(a, b){
65+
const lastGuy = a.passed - a.year;
66+
const nextGuy = b.passed - b.year;
67+
return lastGuy > nextGuy ? -1 : 1;
68+
});
69+
console.table(sortedByYearsLived);
4570

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

74+
// const category = document.querySelector('.mw-category');
75+
// const links = Array.from(category.querySelectorAll('a'));
76+
// const de = links
77+
// .map(link => link.textContent)
78+
// .filter(streetName => streetName.includes('de'));
4979

5080
// 7. sort Exercise
5181
// Sort the people alphabetically by last name
82+
const alpha = people.sort((a, b) => {
83+
const [aLast, aFirst] = a.split(', ');
84+
const [bLast, bFirst] = b.split(', ');
85+
86+
return aLast > bLast ? 1 : -1;
87+
});
88+
console.log(alpha);
5289

5390
// 8. Reduce Exercise
5491
// Sum up the instances of each of these
5592
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
93+
const transportation = data.reduce(function(obj, item){
94+
if(!obj[item]){
95+
obj[item] = 0;
96+
}
97+
obj[item]++;
98+
return obj;
99+
}, {});
100+
console.log(transportation);
56101

57102
</script>
58103
</body>

0 commit comments

Comments
 (0)