Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e67614b

Browse files
Day 04 files
1 parent 3ce1d61 commit e67614b

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<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 src="index.js"></script>
10+
</body>
11+
</html>

‎Day 04 - Array Cardio Day 1/index.js

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Get your shorts on - this is an array workout!
2+
// ## Array Cardio Day 1
3+
4+
// Some data we can work with
5+
6+
const inventors = [
7+
{ first: "Albert", last: "Einstein", year: 1879, passed: 1955 },
8+
{ first: "Isaac", last: "Newton", year: 1643, passed: 1727 },
9+
{ first: "Galileo", last: "Galilei", year: 1564, passed: 1642 },
10+
{ first: "Marie", last: "Curie", year: 1867, passed: 1934 },
11+
{ first: "Johannes", last: "Kepler", year: 1571, passed: 1630 },
12+
{ first: "Nicolaus", last: "Copernicus", year: 1473, passed: 1543 },
13+
{ first: "Max", last: "Planck", year: 1858, passed: 1947 },
14+
{ first: "Katherine", last: "Blodgett", year: 1898, passed: 1979 },
15+
{ first: "Ada", last: "Lovelace", year: 1815, passed: 1852 },
16+
{ first: "Sarah E.", last: "Goode", year: 1855, passed: 1905 },
17+
{ first: "Lise", last: "Meitner", year: 1878, passed: 1968 },
18+
{ first: "Hanna", last: "Hammarström", year: 1829, passed: 1909 },
19+
];
20+
21+
const people = [
22+
"Bernhard, Sandra",
23+
"Bethea, Erin",
24+
"Becker, Carl",
25+
"Bentsen, Lloyd",
26+
"Beckett, Samuel",
27+
"Blake, William",
28+
"Berger, Ric",
29+
"Beddoes, Mick",
30+
"Beethoven, Ludwig",
31+
"Belloc, Hilaire",
32+
"Begin, Menachem",
33+
"Bellow, Saul",
34+
"Benchley, Robert",
35+
"Blair, Robert",
36+
"Benenson, Peter",
37+
"Benjamin, Walter",
38+
"Berlin, Irving",
39+
"Benn, Tony",
40+
"Benson, Leana",
41+
"Bent, Silas",
42+
"Berle, Milton",
43+
"Berry, Halle",
44+
"Biko, Steve",
45+
"Beck, Glenn",
46+
"Bergman, Ingmar",
47+
"Black, Elk",
48+
"Berio, Luciano",
49+
"Berne, Eric",
50+
"Berra, Yogi",
51+
"Berry, Wendell",
52+
"Bevan, Aneurin",
53+
"Ben-Gurion, David",
54+
"Bevel, Ken",
55+
"Biden, Joseph",
56+
"Bennington, Chester",
57+
"Bierce, Ambrose",
58+
"Billings, Josh",
59+
"Birrell, Augustine",
60+
"Blair, Tony",
61+
"Beecher, Henry",
62+
"Biondo, Frank",
63+
];
64+
65+
// Array.prototype.filter()
66+
// 1. Filter the list of inventors for those who were born in the 1500's
67+
const fifteen = inventors.filter(
68+
(inventor) => inventor.year >= 1500 && inventor.year < 1600
69+
);
70+
71+
console.table(fifteen);
72+
73+
// Array.prototype.map()
74+
// 2. Give us an array of the inventors first and last names
75+
const fullNames = inventors.map(
76+
(inventor) => `${inventor.first} ${inventor.last}`
77+
);
78+
console.log(fullNames);
79+
80+
// Array.prototype.sort()
81+
// 3. Sort the inventors by birthdate, oldest to youngest
82+
const ordered = inventors.sort((a, b) => (a.year > b.year ? 1 : -1));
83+
84+
console.table(ordered);
85+
86+
// Array.prototype.reduce()
87+
// 4. How many years did all the inventors live all together?
88+
const totalYears = inventors.reduce((total, inventor) => {
89+
return total + (inventor.passed - inventor.year);
90+
}, 0);
91+
92+
console.log(totalYears);
93+
94+
// 5. Sort the inventors by years lived
95+
const oldest = inventors.sort((a, b) => {
96+
const lastGuy = a.passed - a.year;
97+
const nextGuy = b.passed - b.year;
98+
return lastGuy > nextGuy ? 1 : -1;
99+
});
100+
101+
console.table(oldest);
102+
103+
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
104+
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
105+
// const category = document.querySelector(".mw-category");
106+
// const links = Array.from(category.querySelectorAll("a"));
107+
// const de = links
108+
// .map((link) => link.textContent)
109+
// .filter((streetName) => streetName.includes("de"));
110+
111+
// 7. sort Exercise
112+
// Sort the people alphabetically by last name
113+
const alpha = people.sort((lastOne, nextOne) => {
114+
const [alast, afirst] = lastOne.split(", ");
115+
const [blast, bfirst] = nextOne.split(", ");
116+
return alast > blast ? 1 : -1;
117+
});
118+
119+
console.log(alpha);
120+
121+
// 8. Reduce Exercise
122+
// Sum up the instances of each of these
123+
const data = [
124+
"car",
125+
"car",
126+
"truck",
127+
"truck",
128+
"bike",
129+
"walk",
130+
"car",
131+
"van",
132+
"bike",
133+
"walk",
134+
"car",
135+
"van",
136+
"car",
137+
"truck",
138+
];
139+
140+
const transportation = data.reduce((obj, item) => {
141+
if (!obj[item]) {
142+
obj[item] = 0;
143+
}
144+
obj[item]++;
145+
return obj;
146+
}, {});
147+
148+
console.log(transportation);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /