Skip to content

Commit b99e647

Browse files
committed
finished
1 parent 5c13e5f commit b99e647

1 file changed

Lines changed: 167 additions & 56 deletions

File tree

Lines changed: 167 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,170 @@
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
12-
13-
// Some data we can work with
14-
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-
];
29-
30-
const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black, Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William'];
31-
32-
// Array.prototype.filter()
33-
// 1. Filter the list of inventors for those who were born in the 1500's
34-
35-
// Array.prototype.map()
36-
// 2. Give us an array of the inventors first and last names
37-
38-
// Array.prototype.sort()
39-
// 3. Sort the inventors by birthdate, oldest to youngest
40-
41-
// Array.prototype.reduce()
42-
// 4. How many years did all the inventors live all together?
43-
44-
// 5. Sort the inventors by years lived
45-
46-
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
47-
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
48-
49-
50-
// 7. sort Exercise
51-
// Sort the people alphabetically by last name
52-
53-
// 8. Reduce Exercise
54-
// 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' ];
56-
57-
</script>
58-
</body>
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
12+
13+
// Some data we can work with
14+
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+
];
29+
30+
const people = [
31+
"Beck, Glenn",
32+
"Becker, Carl",
33+
"Beckett, Samuel",
34+
"Beddoes, Mick",
35+
"Beecher, Henry",
36+
"Beethoven, Ludwig",
37+
"Begin, Menachem",
38+
"Belloc, Hilaire",
39+
"Bellow, Saul",
40+
"Benchley, Robert",
41+
"Benenson, Peter",
42+
"Ben-Gurion, David",
43+
"Benjamin, Walter",
44+
"Benn, Tony",
45+
"Bennington, Chester",
46+
"Benson, Leana",
47+
"Bent, Silas",
48+
"Bentsen, Lloyd",
49+
"Berger, Ric",
50+
"Bergman, Ingmar",
51+
"Berio, Luciano",
52+
"Berle, Milton",
53+
"Berlin, Irving",
54+
"Berne, Eric",
55+
"Bernhard, Sandra",
56+
"Berra, Yogi",
57+
"Berry, Halle",
58+
"Berry, Wendell",
59+
"Bethea, Erin",
60+
"Bevan, Aneurin",
61+
"Bevel, Ken",
62+
"Biden, Joseph",
63+
"Bierce, Ambrose",
64+
"Biko, Steve",
65+
"Billings, Josh",
66+
"Biondo, Frank",
67+
"Birrell, Augustine",
68+
"Black, Elk",
69+
"Blair, Robert",
70+
"Blair, Tony",
71+
"Blake, William",
72+
];
73+
74+
// Array.prototype.filter()
75+
// 1. Filter the list of inventors for those who were born in the 1500's
76+
// const fifteen = inventors.filter((inventor) => {
77+
// if (inventor.year >= 1500 && inventor.year < 1600) {
78+
// return true; // keeps the inventor in the array
79+
// }
80+
// });
81+
// console.table(fifteen);
82+
83+
// shortened alternative:
84+
const fifteen = inventors.filter(
85+
(inventor) => inventor.year >= 1500 && inventor.year < 1600
86+
);
87+
console.table(fifteen);
88+
89+
// Array.prototype.map()
90+
// 2. Give us an array of the inventors first and last names
91+
const names = inventors.map(
92+
(inventor) => `${inventor.first} ${inventor.last}`
93+
);
94+
console.log(names);
95+
96+
// Array.prototype.sort()
97+
// 3. Sort the inventors by birthdate, oldest to youngest
98+
// const birthdate = inventors.sort(function (a, b) {
99+
// return a.year - b.year;
100+
// });
101+
// console.table(birthdate);
102+
103+
// shorter alternative:
104+
const birthdate = inventors.sort((a, b) => (a.year > b.year ? 1 : -1));
105+
console.table(birthdate);
106+
107+
// Array.prototype.reduce()
108+
// 4. How many years did all the inventors live all together?
109+
const totalYears = inventors.reduce((total, inventor) => {
110+
return total + (inventor.passed - inventor.year);
111+
}, 0);
112+
console.log(totalYears);
113+
114+
// 5. Sort the inventors by years lived
115+
const oldestInventor = inventors.sort(function (a, b) {
116+
const lastInventor = a.passed - a.year;
117+
const nextInventor = b.passed - b.year;
118+
return lastInventor > nextInventor ? -1 : 1;
119+
});
120+
console.table(oldestInventor);
121+
122+
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
123+
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
124+
const category = document.querySelector(".mw-category");
125+
const links = Array.from(category.querySelectorAll("a"));
126+
127+
const de = links
128+
.map((link) => link.textContent)
129+
.filter((streetName) => streetName.includes("de"));
130+
131+
// 7. sort Exercise
132+
// Sort the people alphabetically by last name
133+
const alpha = people.sort((lastOne, nextOne) => {
134+
const [aLast, aFirst] = lastOne.split(", ");
135+
const [bLast, bFirst] = nextOne.split(", ");
136+
return aLast > bLast ? 1 : -1;
137+
});
138+
console.log(alpha);
139+
140+
// 8. Reduce Exercise
141+
// Sum up the instances of each of these
142+
const data = [
143+
"car",
144+
"car",
145+
"truck",
146+
"truck",
147+
"bike",
148+
"walk",
149+
"car",
150+
"van",
151+
"bike",
152+
"walk",
153+
"car",
154+
"van",
155+
"car",
156+
"truck",
157+
];
158+
159+
const transportation = data.reduce(function (obj, item) {
160+
if (!obj[item]) {
161+
obj[item] = 0;
162+
}
163+
obj[item]++;
164+
return obj;
165+
}, {});
166+
167+
console.log(transportation);
168+
</script>
169+
</body>
59170
</html>

0 commit comments

Comments
 (0)