Skip to content

Commit d05aa96

Browse files
committed
docs: 📝 Learn Array
1 parent a23f7ec commit d05aa96

File tree

2 files changed

+201
-96
lines changed

2 files changed

+201
-96
lines changed
Lines changed: 138 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,147 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8">
5-
<title>Array Cardio 💪</title>
6-
<link rel="icon" href="https://fav.farm/🔥" />
7-
</head>
8-
<body>
9-
<p><em>Psst: have a look at the JavaScript Console</em> 💁</p>
10-
<script>
11-
// Get your shorts on - this is an array workout!
12-
// ## Array Cardio Day 1
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Array Cardio 💪</title>
6+
<link rel="icon" href="https://fav.farm/🔥" />
7+
</head>
8+
<body>
9+
<p><em>Psst: have a look at the JavaScript Console</em> 💁</p>
10+
<script>
11+
// Get your shorts on - this is an array workout!
12+
// ## Array Cardio Day 1
1313

14-
// Some data we can work with
14+
// Some data we can work with
1515

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

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

42-
// Array.prototype.map()
43-
// 2. Give us an array of the inventors first and last names
75+
// Array.prototype.filter()
76+
// 1. Filter the list of inventors for those who were born in the 1500's
77+
const fifteen = inventors.filter(
78+
(inventor) => inventor.year >= 1500 && inventor.year < 1600
79+
);
80+
console.table(fifteen);
81+
// Array.prototype.map()
82+
// 2. Give us an array of the inventors first and last names
83+
const firstLast = inventors.map(
84+
(inventor) => `${inventor.first} ${inventor.last}`
85+
);
86+
console.log(firstLast);
87+
// Array.prototype.sort()
88+
// 3. Sort the inventors by birthdate, oldest to youngest
89+
const sortedInventors = inventors.sort(
90+
(first, second) => first.year - second.year
91+
);
92+
console.log(sortedInventors);
93+
// Array.prototype.reduce()
94+
// 4. How many years did all the inventors live all together?
95+
yearLivedTogether = inventors.reduce(
96+
(total, inventor) => total + (inventor.passed - inventor.year),
97+
0
98+
);
99+
console.log(yearLivedTogether);
100+
// 5. Sort the inventors by years lived
101+
const yearsLived = inventors.sort((first, second) => {
102+
firstLived = first.passed - first.year;
103+
secondLived = second.passed - second.year;
104+
return secondLived > firstLived ? 1 : -1;
105+
});
106+
console.log(yearsLived);
107+
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
108+
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
44109

45-
// Array.prototype.sort()
46-
// 3. Sort the inventors by birthdate, oldest to youngest
110+
// 7. sort Exercise
111+
// Sort the people alphabetically by last name
112+
const alpha = people.sort((lastOne, nextOne) => {
113+
const [aLast, aFirst] = lastOne.split(", ");
114+
const [bLast, bFirst] = nextOne.split(", ");
115+
return aLast > bLast ? 1 : -1;
116+
});
117+
console.log(alpha);
118+
// 8. Reduce Exercise
119+
// Sum up the instances of each of these
120+
const data = [
121+
"car",
122+
"car",
123+
"truck",
124+
"truck",
125+
"bike",
126+
"walk",
127+
"car",
128+
"van",
129+
"bike",
130+
"walk",
131+
"car",
132+
"van",
133+
"car",
134+
"truck",
135+
];
136+
const transportation = data.reduce(function (obj, item) {
137+
if (!obj[item]) {
138+
obj[item] = 0;
139+
}
140+
obj[item]++;
141+
return obj;
142+
}, {});
47143

48-
// Array.prototype.reduce()
49-
// 4. How many years did all the inventors live all together?
50-
51-
// 5. Sort the inventors by years lived
52-
53-
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
54-
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
55-
56-
57-
// 7. sort Exercise
58-
// Sort the people alphabetically by last name
59-
60-
// 8. Reduce Exercise
61-
// Sum up the instances of each of these
62-
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
63-
64-
</script>
65-
</body>
144+
console.log(transportation);
145+
</script>
146+
</body>
66147
</html>
Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,66 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8">
5-
<title>Array Cardio 💪💪</title>
6-
<link rel="icon" href="https://fav.farm/🔥" />
7-
</head>
8-
<body>
9-
<p><em>Psst: have a look at the JavaScript Console</em> 💁</p>
10-
<script>
11-
// ## Array Cardio Day 2
12-
13-
const people = [
14-
{ name: 'Wes', year: 1988 },
15-
{ name: 'Kait', year: 1986 },
16-
{ name: 'Irv', year: 1970 },
17-
{ name: 'Lux', year: 2015 }
18-
];
19-
20-
const comments = [
21-
{ text: 'Love this!', id: 523423 },
22-
{ text: 'Super good', id: 823423 },
23-
{ text: 'You are the best', id: 2039842 },
24-
{ text: 'Ramen is my fav food ever', id: 123523 },
25-
{ text: 'Nice Nice Nice!', id: 542328 }
26-
];
27-
28-
// Some and Every Checks
29-
// Array.prototype.some() // is at least one person 19 or older?
30-
// Array.prototype.every() // is everyone 19 or older?
31-
32-
// Array.prototype.find()
33-
// Find is like filter, but instead returns just the one you are looking for
34-
// find the comment with the ID of 823423
35-
36-
// Array.prototype.findIndex()
37-
// Find the comment with this ID
38-
// delete the comment with the ID of 823423
39-
40-
</script>
41-
</body>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Array Cardio 💪💪</title>
6+
<link rel="icon" href="https://fav.farm/🔥" />
7+
</head>
8+
<body>
9+
<p><em>Psst: have a look at the JavaScript Console</em> 💁</p>
10+
<script>
11+
// ## Array Cardio Day 2
12+
13+
const people = [
14+
{ name: "Wes", year: 1988 },
15+
{ name: "Kait", year: 1986 },
16+
{ name: "Irv", year: 1970 },
17+
{ name: "Lux", year: 2015 },
18+
];
19+
20+
const comments = [
21+
{ text: "Love this!", id: 523423 },
22+
{ text: "Super good", id: 823423 },
23+
{ text: "You are the best", id: 2039842 },
24+
{ text: "Ramen is my fav food ever", id: 123523 },
25+
{ text: "Nice Nice Nice!", id: 542328 },
26+
];
27+
28+
// return true;
29+
// }
30+
// });
31+
32+
const isAdult = people.some(
33+
(person) => new Date().getFullYear() - person.year >= 19
34+
);
35+
36+
console.log({ isAdult });
37+
// Array.prototype.every() // is everyone 19?
38+
39+
const allAdults = people.every(
40+
(person) => new Date().getFullYear() - person.year >= 19
41+
);
42+
console.log({ allAdults });
43+
44+
// Array.prototype.find()
45+
// Find is like filter, but instead returns just the one you are looking for
46+
// find the comment with the ID of 823423
47+
48+
const comment = comments.find((comment) => comment.id === 823423);
49+
50+
console.log(comment);
51+
52+
// Array.prototype.findIndex()
53+
// Find the comment with this ID
54+
// delete the comment with the ID of 823423
55+
const index = comments.findIndex((comment) => comment.id === 823423);
56+
console.log(index);
57+
58+
// comments.splice(index, 1);
59+
60+
const newComments = [
61+
...comments.slice(0, index),
62+
...comments.slice(index + 1),
63+
];
64+
</script>
65+
</body>
4266
</html>

0 commit comments

Comments
 (0)