Skip to content

Commit b35ae98

Browse files
author
yuri12138
committed
第四天
1 parent b154ff9 commit b35ae98

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Array Cardio 💪</title>
7+
</head>
8+
9+
<body>
10+
<p><em>Psst: have a look at the JavaScript Console</em> 💁</p>
11+
<script>
12+
// Get your shorts on - this is an array workout!
13+
// ## Array Cardio Day 1
14+
15+
// Some data we can work with
16+
17+
const inventors = [
18+
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
19+
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
20+
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
21+
{ first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
22+
{ first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
23+
{ first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
24+
{ first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
25+
{ first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
26+
{ first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
27+
{ first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
28+
{ first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
29+
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
30+
];
31+
32+
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'];
33+
34+
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'];
35+
36+
37+
38+
39+
// Array.prototype.filter()
40+
// 1. Filter the list of inventors for those who were born in the 1500's
41+
console.log("Filter the list of inventors for those who were born in the 1500's:");
42+
const a1 = inventors.filter((item) => {
43+
return item.year > 1501 && item.year < 1599;
44+
})
45+
console.table(a1);
46+
47+
// Array.prototype.map()
48+
// 2. Give us an array of the inventors' first and last names
49+
console.log("inventors' first and last names:");
50+
const a2 = inventors.map((item) => {
51+
return item.first + ' ' + item.last;
52+
})
53+
console.log(a2);
54+
55+
// Array.prototype.sort()
56+
// 3. Sort the inventors by birthdate, oldest to youngest
57+
console.log("oldest to youngest:");
58+
function compareFn1(a, b) {
59+
return a.year < b.year ? -1 : 1;
60+
}
61+
inventors.sort(compareFn1);
62+
console.table(inventors);
63+
64+
// Array.prototype.reduce()
65+
// 4. How many years did all the inventors live?
66+
const sum = inventors.reduce((accumulator, currentValue) => {
67+
return accumulator + currentValue.passed - currentValue.year;
68+
}, 0);
69+
console.log(sum);
70+
71+
// 5. Sort the inventors by years lived
72+
console.log("Sort the inventors by years lived:");
73+
function compareFn2(a, b) {
74+
return a.passed - a.year > b.passed - b.year ? -1 : 1;
75+
}
76+
inventors.sort(compareFn2);
77+
console.table(inventors);
78+
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
79+
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
80+
81+
82+
// 7. sort Exercise
83+
// Sort the people alphabetically by last name
84+
console.log("Sort the people alphabetically by last name:");
85+
function compareFn3(a, b) {
86+
return a.split(", ")[1] < b.split(", ")[1] ? -1 : 1;
87+
}
88+
people.sort(compareFn3);
89+
console.log(people);
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+
let res = data.reduce((obj, item) => {
95+
debugger
96+
if(obj[item]){
97+
obj[item] ++;
98+
}else{
99+
obj[item] = 1;
100+
}
101+
return obj;
102+
},{})
103+
console.log(res);
104+
105+
</script>
106+
</body>
107+
108+
</html>

0 commit comments

Comments
 (0)