Skip to content

Commit 7d6c314

Browse files
committed
Array methods part 1
Very simple explanation of some of the most used array methods. Step wesbos#6 was the only step that needed to be done in the console of the browser.
1 parent fdbc11f commit 7d6c314

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

04 - Array Cardio Day 1/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Array Cardio 💪</title>
6+
<link rel="icon" href="https://fav.farm/🔥" />
7+
<link rel="stylesheet" href="./style.css">
8+
<script src="./script.js" defer></script>
9+
</head>
10+
<body>
11+
<p><em>Psst: have a look at the JavaScript Console</em> 💁</p>
12+
</body>
13+
</html>

04 - Array Cardio Day 1/script.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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', 'Bethea, Erin', 'Becker, Carl', 'Bentsen, Lloyd', 'Beckett, Samuel', 'Blake, William', 'Berger, Ric', 'Beddoes, Mick', 'Beethoven, Ludwig',
23+
'Belloc, Hilaire', 'Begin, Menachem', 'Bellow, Saul', 'Benchley, Robert', 'Blair, Robert', 'Benenson, Peter', 'Benjamin, Walter', 'Berlin, Irving',
24+
'Benn, Tony', 'Benson, Leana', 'Bent, Silas', 'Berle, Milton', 'Berry, Halle', 'Biko, Steve', 'Beck, Glenn', 'Bergman, Ingmar', 'Black, Elk', 'Berio, Luciano',
25+
'Berne, Eric', 'Berra, Yogi', 'Berry, Wendell', 'Bevan, Aneurin', 'Ben-Gurion, David', 'Bevel, Ken', 'Biden, Joseph', 'Bennington, Chester', 'Bierce, Ambrose',
26+
'Billings, Josh', 'Birrell, Augustine', 'Blair, Tony', 'Beecher, Henry', 'Biondo, Frank'
27+
];
28+
29+
// Array.prototype.filter()
30+
// 1. Filter the list of inventors for those who were born in the 1500's
31+
const fifteen = inventors.filter( inventor => {
32+
if (inventor.year >= 1500 && inventor.year <1600) return true
33+
});
34+
console.table(fifteen);
35+
36+
// Array.prototype.map()
37+
// 2. Give us an array of the inventors first and last names
38+
const fullNames = inventors.map (inventor => `${inventor.first} ${inventor.last}`)
39+
40+
console.table(fullNames);
41+
42+
43+
// Array.prototype.sort()
44+
// 3. Sort the inventors by birth date, oldest to youngest
45+
const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1)
46+
47+
console.table(ordered)
48+
49+
50+
// Array.prototype.reduce()
51+
// 4. How many years did all the inventors live all together?
52+
const totalYears = inventors.reduce((total, inventor) => {
53+
return total + (inventor.passed - inventor.year)
54+
}, 0)
55+
56+
console.table(totalYears)
57+
58+
// 5. Sort the inventors by years lived
59+
const oldest = inventors.sort (function(a,b) {
60+
const lastGuys = a.passed - a.year;
61+
const nextGuy = b.passed - b.year;
62+
return lastGuys > nextGuy ? -1 : 1;
63+
});
64+
console.table(oldest)
65+
66+
67+
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
68+
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
69+
// const category = document.querySelector('.mw-category');
70+
// const links = Array.from(category.querySelectorAll('a'));
71+
72+
// const de = links
73+
// .map(link => link.textContent)
74+
// .filter(streetName => streetName.includes('de'))
75+
76+
77+
78+
// 7. sort Exercise
79+
// Sort the people alphabetically by last name
80+
81+
const alpha = people.sort((lastOne, nextOne) => {
82+
const [alast, afirst] = lastOne.split(', ');
83+
const [blast, bfirst] = nextOne.split(', ');
84+
return alast > blast ? 1 : -1;
85+
})
86+
87+
console.log(alpha)
88+
89+
// 8. Reduce Exercise
90+
// Sum up the instances of each of these
91+
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
92+
93+
const transportation = data.reduce(function(obj, item) {
94+
if (!obj[item]) {
95+
obj[item] = 0;
96+
}
97+
obj[item]++;
98+
return obj;
99+
}, {})
100+
101+
console.log(transportation)

04 - Array Cardio Day 1/style.css

Whitespace-only changes.

0 commit comments

Comments
 (0)