Skip to content

Commit 1e7b874

Browse files
committed
Completed wesbos#4
1 parent 7afd1b1 commit 1e7b874

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,85 @@
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(inventor => (inventor.year >= 1500 && inventor.year < 1600));
35+
// console.log(fifteen);
36+
console.table(fifteen);
3437

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

3844
// Array.prototype.sort()
3945
// 3. Sort the inventors by birthdate, oldest to youngest
4046

47+
// const ordered = inventors.sort(function(a, b) {
48+
// if (a.year > b.year) {
49+
// // move up
50+
// return 1;
51+
// } else {
52+
// // move down
53+
// return -1;
54+
// }
55+
// });
56+
57+
const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1);
58+
// console.log(ordered);
59+
console.table(ordered);
60+
4161
// Array.prototype.reduce()
4262
// 4. How many years did all the inventors live?
63+
const totalYears = inventors.reduce((total, inventor) => {
64+
return total + (inventor.passed - inventor.year);
65+
}, 0);
66+
console.log(totalYears);
4367

4468
// 5. Sort the inventors by years lived
69+
// const yearsSorted = inventors.sort(function(a, b) {
70+
// if ((a.passed - a.year) > (b.passed - b.year)) {
71+
// return 1;
72+
// } else {
73+
// return -1;
74+
// }
75+
// });
76+
77+
const yearsSorted = inventors.sort((a, b) => (a.passed - a.year) > (b.passed - b.year) ? -1 : 1);
78+
console.table(yearsSorted);
4579

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

83+
// const category = document.querySelector('.mw-category');
84+
// const links = Array.from(category.querySelectorAll('a'));
85+
// const de = links
86+
// .map(link => link.textContent)
87+
// .filter(streetName => streetName.includes('de'));
4988

5089
// 7. sort Exercise
5190
// Sort the people alphabetically by last name
91+
const alpha = people.sort(function(lastOne, nextOne) {
92+
const [aLast, aFirst] = lastOne.split(', ');
93+
const [bLast, bFirst] = nextOne.split(', ');
94+
return aLast > bLast ? 1 : -1;
95+
});
96+
// console.log(alpha);
97+
console.table(alpha);
5298

5399
// 8. Reduce Exercise
54100
// Sum up the instances of each of these
55-
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
101+
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck', 'pogo stick' ];
102+
103+
const transport = data.reduce(function(obj, item) {
104+
if (!obj[item]) {
105+
obj[item] = 0;
106+
}
107+
obj[item]++;
108+
return obj;
109+
}, {});
110+
111+
// console.log(transport);
112+
console.table(transport);
56113

57114
</script>
58115
</body>

0 commit comments

Comments
 (0)