Skip to content

Commit 0a8ad7a

Browse files
committed
feat: day 4
1 parent b2cc4a6 commit 0a8ad7a

1 file changed

Lines changed: 151 additions & 50 deletions

File tree

Lines changed: 151 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,166 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8">
5-
<title>Array Cardio 💪</title>
6-
</head>
7-
<body>
8-
<p><em>Psst: have a look at the JavaScript Console</em> 💁</p>
9-
<script>
10-
// Get your shorts on - this is an array workout!
11-
// ## Array Cardio Day 1
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Array Cardio 💪</title>
6+
</head>
7+
<body>
8+
<p><em>Psst: have a look at the JavaScript Console</em> 💁</p>
9+
<script>
10+
// Get your shorts on - this is an array workout!
11+
// ## Array Cardio Day 1
1212

13-
// Some data we can work with
13+
// Some data we can work with
1414

15-
const inventors = [
16-
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
17-
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
18-
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
19-
{ first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
20-
{ first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
21-
{ first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
22-
{ first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
23-
{ first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
24-
{ first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
25-
{ first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
26-
{ first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
27-
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
28-
];
15+
const inventors = [
16+
{ first: "Albert", last: "Einstein", year: 1879, passed: 1955 },
17+
{ first: "Isaac", last: "Newton", year: 1643, passed: 1727 },
18+
{ first: "Galileo", last: "Galilei", year: 1564, passed: 1642 },
19+
{ first: "Marie", last: "Curie", year: 1867, passed: 1934 },
20+
{ first: "Johannes", last: "Kepler", year: 1571, passed: 1630 },
21+
{ first: "Nicolaus", last: "Copernicus", year: 1473, passed: 1543 },
22+
{ first: "Max", last: "Planck", year: 1858, passed: 1947 },
23+
{ first: "Katherine", last: "Blodgett", year: 1898, passed: 1979 },
24+
{ first: "Ada", last: "Lovelace", year: 1815, passed: 1852 },
25+
{ first: "Sarah E.", last: "Goode", year: 1855, passed: 1905 },
26+
{ first: "Lise", last: "Meitner", year: 1878, passed: 1968 },
27+
{ first: "Hanna", last: "Hammarström", year: 1829, passed: 1909 },
28+
];
2929

30-
const people = [
31-
'Bernhard, Sandra', 'Bethea, Erin', 'Becker, Carl', 'Bentsen, Lloyd', 'Beckett, Samuel', 'Blake, William', 'Berger, Ric', 'Beddoes, Mick', 'Beethoven, Ludwig',
32-
'Belloc, Hilaire', 'Begin, Menachem', 'Bellow, Saul', 'Benchley, Robert', 'Blair, Robert', 'Benenson, Peter', 'Benjamin, Walter', 'Berlin, Irving',
33-
'Benn, Tony', 'Benson, Leana', 'Bent, Silas', 'Berle, Milton', 'Berry, Halle', 'Biko, Steve', 'Beck, Glenn', 'Bergman, Ingmar', 'Black, Elk', 'Berio, Luciano',
34-
'Berne, Eric', 'Berra, Yogi', 'Berry, Wendell', 'Bevan, Aneurin', 'Ben-Gurion, David', 'Bevel, Ken', 'Biden, Joseph', 'Bennington, Chester', 'Bierce, Ambrose',
35-
'Billings, Josh', 'Birrell, Augustine', 'Blair, Tony', 'Beecher, Henry', 'Biondo, Frank'
36-
];
37-
38-
// Array.prototype.filter()
39-
// 1. Filter the list of inventors for those who were born in the 1500's
30+
const people = [
31+
"Bernhard, Sandra",
32+
"Bethea, Erin",
33+
"Becker, Carl",
34+
"Bentsen, Lloyd",
35+
"Beckett, Samuel",
36+
"Blake, William",
37+
"Berger, Ric",
38+
"Beddoes, Mick",
39+
"Beethoven, Ludwig",
40+
"Belloc, Hilaire",
41+
"Begin, Menachem",
42+
"Bellow, Saul",
43+
"Benchley, Robert",
44+
"Blair, Robert",
45+
"Benenson, Peter",
46+
"Benjamin, Walter",
47+
"Berlin, Irving",
48+
"Benn, Tony",
49+
"Benson, Leana",
50+
"Bent, Silas",
51+
"Berle, Milton",
52+
"Berry, Halle",
53+
"Biko, Steve",
54+
"Beck, Glenn",
55+
"Bergman, Ingmar",
56+
"Black, Elk",
57+
"Berio, Luciano",
58+
"Berne, Eric",
59+
"Berra, Yogi",
60+
"Berry, Wendell",
61+
"Bevan, Aneurin",
62+
"Ben-Gurion, David",
63+
"Bevel, Ken",
64+
"Biden, Joseph",
65+
"Bennington, Chester",
66+
"Bierce, Ambrose",
67+
"Billings, Josh",
68+
"Birrell, Augustine",
69+
"Blair, Tony",
70+
"Beecher, Henry",
71+
"Biondo, Frank",
72+
];
4073

41-
// Array.prototype.map()
42-
// 2. Give us an array of the inventors first and last names
74+
// Array.prototype.filter()
75+
// 1. Filter the list of inventors for those who were born in the 1500's
76+
const solution1 = inventors.filter(
77+
(inventor) => inventor.year >= 1500 && inventor.year < 1600
78+
);
79+
console.table(solution1);
4380

44-
// Array.prototype.sort()
45-
// 3. Sort the inventors by birthdate, oldest to youngest
81+
// Array.prototype.map()
82+
// 2. Give us an array of the inventors first and last names
83+
const solution2 = inventors.map(
84+
(inventor) => `${inventor.first} ${inventor.last}`
85+
);
86+
console.table(solution2);
4687

47-
// Array.prototype.reduce()
48-
// 4. How many years did all the inventors live all together?
88+
// Array.prototype.sort()
89+
// 3. Sort the inventors by birthdate, oldest to youngest
90+
const solution3 = inventors.sort((a, b) => {
91+
if (a.year < b.year) {
92+
return -1;
93+
}
4994

50-
// 5. Sort the inventors by years lived
95+
return 1;
96+
});
97+
console.table(solution3);
5198

52-
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
53-
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
99+
// Array.prototype.reduce()
100+
// 4. How many years did all the inventors live all together?
101+
const solution4 = inventors.reduce((sum, inventor) => {
102+
const inventorAge = inventor.passed - inventor.year;
103+
return sum + inventorAge;
104+
}, 0);
105+
console.log(solution4);
54106

107+
// 5. Sort the inventors by years lived
108+
const solution5 = inventors.sort((a, b) => {
109+
const aLived = a.passed - a.year;
110+
const bLived = b.passed - b.year;
55111

56-
// 7. sort Exercise
57-
// Sort the people alphabetically by last name
112+
if (aLived < bLived) {
113+
return -1;
114+
}
115+
return 1;
116+
});
117+
console.table(solution5);
58118

59-
// 8. Reduce Exercise
60-
// Sum up the instances of each of these
61-
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
119+
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
120+
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
121+
/*
122+
Array.from(document.querySelectorAll(".mw-category-group li a"))
123+
.map((link) => link.textContent)
124+
.filter((boulevard) => boulevard.includes(" de "));
125+
*/
62126

63-
</script>
64-
</body>
127+
// 7. sort Exercise
128+
// Sort the people alphabetically by last name
129+
const solution7 = people.sort((a, b) => {
130+
const [aName] = a.split(", ");
131+
const [bName] = b.split(", ");
132+
133+
if (aName > bName) {
134+
return 1;
135+
}
136+
137+
return -1;
138+
});
139+
console.log(solution7);
140+
141+
// 8. Reduce Exercise
142+
// Sum up the instances of each of these
143+
const data = [
144+
"car",
145+
"car",
146+
"truck",
147+
"truck",
148+
"bike",
149+
"walk",
150+
"car",
151+
"van",
152+
"bike",
153+
"walk",
154+
"car",
155+
"van",
156+
"car",
157+
"truck",
158+
];
159+
const solution8 = data.reduce((instances, item) => {
160+
instances[item] = instances[item] ? instances[item] + 1 : 1;
161+
return instances;
162+
}, {});
163+
console.table(solution8);
164+
</script>
165+
</body>
65166
</html>

0 commit comments

Comments
 (0)