Skip to content

Commit 598974e

Browse files
committed
challenge 4 is completed. Array methods.
1 parent 4148b1a commit 598974e

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,62 @@
3232

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

3638
// Array.prototype.map()
3739
// 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', fullNames);
3842

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

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

4555
// 5. Sort the inventors by years lived
56+
const oldest = inventors.sort(function(a, b) {
57+
const lastGuy = a.passed - a.year;
58+
const nextGuy = b.passed - b.year;
59+
return lastGuy > nextGuy ? -1 : 1;
60+
});
61+
console.table(oldest);
4662

4763
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4864
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
49-
65+
/* const category = document.querySelector('.mw-category');
66+
const links = Array.from(category.querySelectorAll('a'));
67+
const de = links
68+
.map(link => link.textContent)
69+
.filter(streetName => streetName.includes('de'));*/
5070

5171
// 7. sort Exercise
5272
// Sort the people alphabetically by last name
73+
const alpha = people.sort((a, b) => {
74+
const [aLast, aFirst] = a.split(', ');
75+
const [bLast, bFirst] = b.split(', ');
76+
return aLast > bLast ? 1 : -1;
77+
});
78+
console.log('alpha', alpha);
5379

5480
// 8. Reduce Exercise
5581
// Sum up the instances of each of these
5682
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
83+
const transportation = data.reduce((obj, item) => {
84+
if (!obj[item]) {
85+
obj[item] = 0;
86+
}
87+
obj[item]++;
88+
return obj;
89+
}, {});
90+
console.log('transportation', transportation);
5791

5892
</script>
5993
</body>

0 commit comments

Comments
 (0)