Skip to content

Commit 89a320d

Browse files
committed
day 4 - array methods cardio!
1 parent d6a38d6 commit 89a320d

File tree

1 file changed

+74
-28
lines changed

1 file changed

+74
-28
lines changed
+74-28
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<meta charset="UTF-8">
5-
<title>Array Cardio 💪</title>
4+
<meta charset="UTF-8">
5+
<title>Array Cardio 💪</title>
66
</head>
77
<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
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
1212

13-
// Some data we can work with
13+
// Some data we can work with
1414

15-
const inventors = [
15+
const inventors = [
1616
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
1717
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
1818
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
@@ -25,37 +25,83 @@
2525
{ first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
2626
{ first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
2727
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
28-
];
28+
];
2929

30-
const flavours = ['Chocolate Chip', 'Kulfi', 'Caramel Praline', 'Chocolate', 'Burnt Caramel', 'Pistachio', 'Rose', 'Sweet Coconut', 'Lemon Cookie', 'Toffeeness', 'Toasted Almond', 'Black Raspberry Crunch', 'Chocolate Brownies', 'Pistachio Almond', 'Strawberry', 'Lavender Honey', 'Lychee', 'Peach', 'Black Walnut', 'Birthday Cake', 'Mexican Chocolate', 'Mocha Almond Fudge', 'Raspberry'];
30+
const flavours = ['Chocolate Chip', 'Kulfi', 'Caramel Praline', 'Chocolate', 'Burnt Caramel', 'Pistachio', 'Rose', 'Sweet Coconut', 'Lemon Cookie', 'Toffeeness', 'Toasted Almond', 'Black Raspberry Crunch', 'Chocolate Brownies', 'Pistachio Almond', 'Strawberry', 'Lavender Honey', 'Lychee', 'Peach', 'Black Walnut', 'Birthday Cake', 'Mexican Chocolate', 'Mocha Almond Fudge', 'Raspberry'];
3131

32-
const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William'];
32+
const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William'];
3333

34-
// Array.prototype.filter()
35-
// 1. Filter the list of inventors for those who were born in the 1500's
34+
// Array.prototype.filter()
35+
// 1. Filter the list of inventors for those who were born in the 1500's
36+
const bornInSixteenth = inventors.filter(element =>
37+
(element.year >= 1500 && element.year<1600 ) //if it returns true, records are kept
38+
);
3639

37-
// Array.prototype.map()
38-
// 2. Give us an array of the inventors' first and last names
40+
// Array.prototype.map()
41+
// 2. Give us an array of the inventors' first and last names
42+
//inny zapis:
43+
const fullNames = inventors.map(function(element){return `${element.first} ${element.last}`})
3944

40-
// Array.prototype.sort()
41-
// 3. Sort the inventors by birthdate, oldest to youngest
45+
// Array.prototype.sort()
46+
// 3. Sort the inventors by birthdate, oldest to youngest
4247

43-
// Array.prototype.reduce()
44-
// 4. How many years did all the inventors live?
48+
const ordered = inventors.sort((a,b) => a.year-b.year);
49+
console.table(ordered);
50+
// Array.prototype.reduce()
51+
// 4. How many years did all the inventors live?
4552

46-
// 5. Sort the inventors by years lived
53+
const totalYears = inventors.reduce(function(total, inventor){
54+
return total + (inventor.passed - inventor.year);
55+
},0);
4756

48-
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
49-
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
57+
console.log(totalYears);
5058

59+
// 5. Sort the inventors by years lived
5160

52-
// 7. sort Exercise
53-
// Sort the people alphabetically by last name
61+
const sortedLived = inventors.sort((a,b) => {
62+
const lastAge = a.passed - a.year;
63+
const nextAge = b.passed - b.year;
5464

55-
// 8. Reduce Exercise
56-
// Sum up the instances of each of these
57-
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
65+
return lastAge - nextAge;
66+
});
5867

59-
</script>
68+
console.table(sortedLived);
69+
70+
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
71+
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
72+
//
73+
// const list = Array.from(document.querySelector('.mw-category').querySelectorAll('a'));
74+
// const streetNames = list.map(link => link.textContent);
75+
// console.log(streetNames);
76+
// const filterStreet = streetNames.filter(element => element.includes('de'));
77+
78+
79+
// 7. sort Exercise
80+
// Sort the people alphabetically by last name
81+
82+
const sortedPeople = people.sort(function(a,b){
83+
let [a_lastName, a_firstName] = a.split(', ');
84+
let [b_lastName, b_firstName] = b.split(', ');
85+
86+
return a_lastName > b_lastName ? 1 : -1;
87+
88+
});
89+
console.log(sortedPeople);
90+
91+
// 8. Reduce Exercise
92+
// Sum up the instances of each of these
93+
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
94+
95+
const transportation = data.reduce(function(object, item){
96+
97+
if (!object[item]) {
98+
object[item] = 0;
99+
}
100+
object[item]++;
101+
return object;
102+
},{});
103+
console.log(transportation);
104+
105+
</script>
60106
</body>
61107
</html>

0 commit comments

Comments
 (0)