Skip to content

Commit 12e8494

Browse files
committed
arrmethods
ARRR MATEY
1 parent c2f3415 commit 12e8494

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

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

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<body>
1010
<script>
11+
'use strict';
1112
// Get your shorts on - this is an array workout!
1213
// ## Array Cardio Day 1
1314

@@ -29,65 +30,74 @@
2930

3031
// Array.prototype.filter()
3132
// 1. Filter the list of inventors for those who were born in the 1500's
32-
const inventors_from_1500s = inventors.filter(
33-
(inventor) => inventor.year >= 1500 && inventor.year < 1600
34-
)
33+
const inventors_from_1500s = inventors.filter(({year}) => year >= 1500 && year < 1600);
3534

36-
console.table(inventors_from_1500s)
35+
console.table(inventors_from_1500s);
3736

3837
// Array.prototype.map()
3938
// 2. Give us an array of the inventors first and last names
40-
const inventors_full_name = inventors.map((inventor) => `${inventor.first} ${inventor.last}`)
41-
console.table(inventors_full_name)
39+
const inventors_full_name = inventors.map(({first, last}) => first + ' ' + last);
40+
console.log(inventors_full_name);
4241

4342
// Array.prototype.sort()
4443
// 3. Sort the inventors by birthdate, oldest to youngest
45-
const inventors_by_date = inventors.sort((a, b) => a.year - b.year)
46-
console.table(inventors_by_date)
44+
const inventors_by_date = inventors.sort(({year: a}, {year: b}) => a - b);
45+
console.table(inventors_by_date);
4746

4847
// Array.prototype.reduce()
4948
// 4. How many years did all the inventors live?
5049
const total_years_lived = inventors.reduce(
51-
(currValue, inventor) => currValue += (inventor.passed - inventor.year), 0
52-
)
50+
(currValue, {passed, year}) => currValue += (passed - year), 0
51+
);
5352
console.log(total_years_lived);
5453

5554
// 5. Sort the inventors by years lived
5655
const inventors_by_lifespan = inventors.sort(
57-
(a, b) => (a.passed - a.year) - (b.passed - b.year)
56+
({passed: aP, year: aY}, {passed: bP, year: bY}) => (aP - aY) - (bP - bY)
5857
);
5958
console.table(inventors_by_lifespan);
6059

6160
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
6261
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
6362
/*
64-
This code will be run directly from the browser console
65-
after navigating to the Wikipedia link above.
66-
63+
// This code will be run directly from the browser console
64+
// after navigating to the Wikipedia link above.
65+
6766
const blvd_with_de = Array.from(document.querySelectorAll('.mw-category a'))
68-
.map(link => link.textContent)
67+
.map(({textContent}) => textContent)
6968
.filter((blvd) => blvd.includes('de'))
70-
*/
7169
70+
*/
7271
// 7. sort Exercise
7372
// Sort the people alphabetically by last name
74-
7573
const people_by_last_name = people.sort((a, b) => a.split(', ')[0] < b.split(', ')[0] ? -1 : 1);
76-
7774
console.log(people_by_last_name);
75+
7876
// 8. Reduce Exercise
7977
// Sum up the instances of each of these
80-
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck'];
78+
const data = [
79+
'car',
80+
'car',
81+
'truck',
82+
'truck',
83+
'bike',
84+
'walk',
85+
'car',
86+
'van',
87+
'bike',
88+
'walk',
89+
'car',
90+
'van',
91+
'car',
92+
'truck'];
8193

8294
const data_instance_count = data.reduce((tallyObject, item) => {
83-
if (!tallyObject[item]) {
84-
tallyObject[item] = 0;
85-
}
86-
tallyObject[item]++;
87-
return tallyObject;
95+
tallyObject[item] ? tallyObject[item]++ : tallyObject[item] = 1;
96+
97+
return tallyObject;
8898
}, {});
99+
console.dir(data_instance_count);
89100

90-
console.log(data_instance_count);
91101
</script>
92102
</body>
93103

0 commit comments

Comments
 (0)