Visitar URL original
JS30 #4 complete · DeonJordaan/JavaScript30@8d5a36c · GitHub
Skip to content

Commit 8d5a36c

Browse files
committed
JS30 wesbos#4 complete
1 parent 9779a81 commit 8d5a36c

1 file changed

Lines changed: 188 additions & 62 deletions

File tree

Lines changed: 188 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,191 @@
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 = [
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
40-
41-
// Array.prototype.map()
42-
// 2. Give us an array of the inventors first and last names
43-
44-
// Array.prototype.sort()
45-
// 3. Sort the inventors by birthdate, oldest to youngest
46-
47-
// Array.prototype.reduce()
48-
// 4. How many years did all the inventors live all together?
49-
50-
// 5. Sort the inventors by years lived
51-
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
54-
55-
56-
// 7. sort Exercise
57-
// Sort the people alphabetically by last name
58-
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' ];
62-
63-
</script>
64-
</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+
{
22+
first: "Nicolaus",
23+
last: "Copernicus",
24+
year: 1473,
25+
passed: 1543,
26+
},
27+
{ first: "Max", last: "Planck", year: 1858, passed: 1947 },
28+
{
29+
first: "Katherine",
30+
last: "Blodgett",
31+
year: 1898,
32+
passed: 1979,
33+
},
34+
{ first: "Ada", last: "Lovelace", year: 1815, passed: 1852 },
35+
{ first: "Sarah E.", last: "Goode", year: 1855, passed: 1905 },
36+
{ first: "Lise", last: "Meitner", year: 1878, passed: 1968 },
37+
{
38+
first: "Hanna",
39+
last: "Hammarström",
40+
year: 1829,
41+
passed: 1909,
42+
},
43+
];
44+
45+
const people = [
46+
"Bernhard, Sandra",
47+
"Bethea, Erin",
48+
"Becker, Carl",
49+
"Bentsen, Lloyd",
50+
"Beckett, Samuel",
51+
"Blake, William",
52+
"Berger, Ric",
53+
"Beddoes, Mick",
54+
"Beethoven, Ludwig",
55+
"Belloc, Hilaire",
56+
"Begin, Menachem",
57+
"Bellow, Saul",
58+
"Benchley, Robert",
59+
"Blair, Robert",
60+
"Benenson, Peter",
61+
"Benjamin, Walter",
62+
"Berlin, Irving",
63+
"Benn, Tony",
64+
"Benson, Leana",
65+
"Bent, Silas",
66+
"Berle, Milton",
67+
"Berry, Halle",
68+
"Biko, Steve",
69+
"Beck, Glenn",
70+
"Bergman, Ingmar",
71+
"Black, Elk",
72+
"Berio, Luciano",
73+
"Berne, Eric",
74+
"Berra, Yogi",
75+
"Berry, Wendell",
76+
"Bevan, Aneurin",
77+
"Ben-Gurion, David",
78+
"Bevel, Ken",
79+
"Biden, Joseph",
80+
"Bennington, Chester",
81+
"Bierce, Ambrose",
82+
"Billings, Josh",
83+
"Birrell, Augustine",
84+
"Blair, Tony",
85+
"Beecher, Henry",
86+
"Biondo, Frank",
87+
];
88+
89+
// Array.prototype.filter()
90+
// 1. Filter the list of inventors for those who were born in the 1500's
91+
const midAgers = inventors.filter(
92+
(inventor) => inventor.year >= 1500 && inventor.year <= 1599
93+
);
94+
console.table(midAgers);
95+
96+
// Array.prototype.map()
97+
// 2. Give us an array of the inventors first and last names
98+
const names = inventors.map(
99+
// (names) => names.first + " " + names.last // Option 1
100+
(names) => `${names.first} ${names.last}` // Same but with template literal
101+
);
102+
103+
console.log(names);
104+
105+
// Array.prototype.sort()
106+
// 3. Sort the inventors by birthdate, oldest to youngest
107+
108+
const bornFirst = inventors.sort((a, b) => a.year - b.year);
109+
110+
//Wes' Solution with ternary operator
111+
// const bornFirst = inventors.sort((a, b) =>
112+
// a.year > b.year ? 1 : -1
113+
// );
114+
115+
console.table(bornFirst);
116+
117+
// Array.prototype.reduce()
118+
// 4. How many years did all the inventors live all together?
119+
120+
const totalYearsLived = inventors.reduce(
121+
(years, age) => years + (age.passed - age.year),
122+
0
123+
);
124+
125+
console.log(totalYearsLived);
126+
127+
// 5. Sort the inventors by years lived
128+
129+
const lived = inventors.sort(
130+
(a, b) => b.passed - b.year - (a.passed - a.year)
131+
);
132+
133+
console.table(lived);
134+
135+
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
136+
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
137+
138+
//NOTE You have to run this one in the dev console while above link is open
139+
// const category = document.querySelector(".mw-category");
140+
// const links = Array.from(category.querySelectorAll("a"));
141+
// const de = links
142+
// .map((link) => link.textContent)
143+
// .filter((streetName) => streetName.includes("de"));
144+
145+
// 7. sort Exercise
146+
// Sort the people alphabetically by last name
147+
148+
const namesABC = people.sort();
149+
console.log(namesABC);
150+
151+
//NOTE I DO NOT understand why Wes did it THIS way...?
152+
153+
const alpha = people.sort((lastOne, nextOne) => {
154+
const [aLast, aFirst] = lastOne.split(", ");
155+
const [bLast, bFirst] = nextOne.split(", ");
156+
return aLast > bLast ? 1 : -1;
157+
});
158+
console.log(alpha);
159+
160+
// 8. Reduce Exercise
161+
// Sum up the instances of each of these
162+
const data = [
163+
"car",
164+
"car",
165+
"truck",
166+
"truck",
167+
"bike",
168+
"walk",
169+
"car",
170+
"van",
171+
"bike",
172+
"walk",
173+
"car",
174+
"van",
175+
"car",
176+
"truck",
177+
"pogostick",
178+
];
179+
180+
const summary = data.reduce((acc, vehicle) => {
181+
if (!acc[vehicle]) {
182+
acc[vehicle] = 0;
183+
}
184+
acc[vehicle]++;
185+
return acc;
186+
}, {});
187+
188+
console.log(summary);
189+
</script>
190+
</body>
65191
</html>

0 commit comments

Comments
 (0)