Skip to content

Commit 05a7d98

Browse files
committed
Day 4.
1 parent 9594a72 commit 05a7d98

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

04 - Array Cardio Day 1/data.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const inventors = [
2+
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
3+
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
4+
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
5+
{ first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
6+
{ first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
7+
{ first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
8+
{ first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
9+
{ first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
10+
{ first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
11+
{ first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
12+
{ first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
13+
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
14+
];
15+
16+
const people = [
17+
'Beck, Glenn',
18+
'Becker, Carl',
19+
'Beckett, Samuel',
20+
'Beddoes, Mick',
21+
'Beecher, Henry',
22+
'Beethoven, Ludwig',
23+
'Begin, Menachem',
24+
'Belloc, Hilaire',
25+
'Bellow, Saul',
26+
'Benchley, Robert',
27+
'Benenson, Peter',
28+
'Ben-Gurion, David',
29+
'Benjamin, Walter',
30+
'Benn, Tony',
31+
'Bennington, Chester',
32+
'Benson, Leana',
33+
'Bent, Silas',
34+
'Bentsen, Lloyd',
35+
'Berger, Ric',
36+
'Bergman, Ingmar',
37+
'Berio, Luciano',
38+
'Berle, Milton',
39+
'Berlin, Irving',
40+
'Berne, Eric',
41+
'Bernhard, Sandra',
42+
'Berra, Yogi',
43+
'Berry, Halle',
44+
'Berry, Wendell',
45+
'Bethea, Erin',
46+
'Bevan, Aneurin',
47+
'Bevel, Ken',
48+
'Biden, Joseph',
49+
'Bierce, Ambrose',
50+
'Biko, Steve',
51+
'Billings, Josh',
52+
'Biondo, Frank',
53+
'Birrell, Augustine',
54+
'Black, Elk',
55+
'Blair, Robert',
56+
'Blair, Tony',
57+
'Blake, William'
58+
];
59+
60+
const data = [
61+
'car',
62+
'car',
63+
'truck',
64+
'truck',
65+
'bike',
66+
'walk',
67+
'car',
68+
'van',
69+
'bike',
70+
'walk',
71+
'car',
72+
'van',
73+
'car',
74+
'truck'
75+
];

04 - Array Cardio Day 1/index.html

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Array Cardio 💪</title>
6+
<script src="data.js"></script>
7+
</head>
8+
<body>
9+
10+
<p><em>Psst: have a look at the JavaScript Console</em> 💁</p>
11+
12+
<script>
13+
// Array.prototype.filter()
14+
// 1. Filter the list of inventors for those who were born in the 1500's
15+
console.log(
16+
inventors.filter( ( { year } ) => ( 1499 < year && year < 1600 ) )
17+
);
18+
19+
// Array.prototype.map()
20+
// 2. Give us an array of the inventors' first and last names
21+
console.log(
22+
inventors.map( ( { first, last } ) => `${first} ${last}` )
23+
);
24+
25+
// Array.prototype.sort()
26+
// 3. Sort the inventors by birthdate, oldest to youngest
27+
console.log(
28+
inventors.sort( ( { year: a }, { year: b } ) => ( a - b ) )
29+
);
30+
31+
// Array.prototype.reduce()
32+
// 4. How many years did all the inventors live?
33+
console.log(
34+
inventors.reduce( ( sum, { year, passed } ) => ( sum + ( passed - year ) ), 0 )
35+
);
36+
37+
// 5. Sort the inventors by years lived
38+
console.log(
39+
inventors.sort( ( a, b ) => ( ( a.passed - a.year ) - ( b.passed - b.year ) ) )
40+
);
41+
42+
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
43+
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
44+
// TODO: Paste the following code in your console on the Wikipedia page:
45+
// Array.from( document.querySelector( '.mw-category' ).getElementsByTagName( 'a' ) )
46+
// .map( ( el ) => el.text )
47+
// .filter( ( name ) => name.includes( 'de' ) );
48+
49+
// 7. sort Exercise
50+
// Sort the people alphabetically by last name
51+
console.log(
52+
people.sort( ( a, b ) => ( a.split( ',' )[0] > b.split( ',' )[0] ) )
53+
);
54+
55+
// 8. Reduce Exercise
56+
// Sum up the instances of each of these
57+
console.log(
58+
data.reduce( ( sums, item ) => {
59+
sums[ item ] = ( sums[ item ] || 0 ) + 1;
60+
61+
return sums;
62+
}, {} )
63+
);
64+
</script>
65+
66+
</body>
67+
</html>

0 commit comments

Comments
 (0)