Skip to content

Commit 968d445

Browse files
author
Kirk byers
committed
Complete day 4
1 parent 269ba2e commit 968d445

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,69 @@
2727

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

3133
// Array.prototype.map()
3234
// 2. Give us an array of the inventory first and last names
35+
const invFirstLast = inventors.map(inventor => Object.assign({}, {first: inventor.first, last: inventor.last}));
36+
console.log(invFirstLast);
3337

3438
// Array.prototype.sort()
3539
// 3. Sort the inventors by birthdate, oldest to youngest
40+
const invSortBirth = inventors.sort((a, b) => a.year - b.year);
41+
console.log(invSortBirth);
3642

3743
// Array.prototype.reduce()
3844
// 4. How many years did all the inventors live?
45+
const totalYears = inventors.reduce((total, inventor) => {
46+
return total + (inventor.passed - inventor.year);
47+
}, 0)
48+
console.log(totalYears);
3949

4050
// 5. Sort the inventors by years lived
51+
const invSortLived = inventors.sort((a, b) => (a.passed - a.year) - (b.passed - b.year));
52+
console.log(invSortLived);
4153

4254
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4355
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
56+
/**
57+
const div = document.querySelector('.mw-category');
58+
const streets = Array.from(div.querySelectorAll('a'));
59+
const de = streets
60+
.map(street => street.textContent)
61+
.filter(street => street.included('de'))
62+
63+
console.log(de);
64+
*/
4465

4566

4667
// 7. sort Exercise
4768
// Sort the people alphabetically by last name
69+
const peopSort = people.sort((a, b) => {
70+
const [prevLast, prevFirst] = a.split(', ');
71+
const [nextLast, nextFirst] = b.split(', ');
72+
73+
return prevLast > nextLast ? 1 : -1;
74+
});
75+
76+
console.log(peopSort);
4877

4978
// 8. Reduce Exercise
5079
// Sum up the instances of each of these
5180
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
5281

82+
const counts = data.reduce((dataCount, entry) => {
83+
if (dataCount[entry]) {
84+
dataCount[entry] ++;
85+
} else {
86+
dataCount[entry] = 1;
87+
}
88+
return dataCount;
89+
}, {});
90+
91+
console.log(counts);
92+
5393
</script>
5494
</body>
5595
</html>

0 commit comments

Comments
 (0)