Visitar URL original
Lesson 04 - complete array exercises · wesbos/JavaScript30@6d15b3d · GitHub
Skip to content

Commit 6d15b3d

Browse files
committed
Lesson 04 - complete array exercises
1 parent 0b8cf75 commit 6d15b3d

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎04 - Array Cardio Day 1/index-START.html‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,69 @@
3131

3232
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+
console.clear()
35+
3436
// Array.prototype.filter()
3537
// 1. Filter the list of inventors for those who were born in the 1500's
38+
console.log(
39+
'inventors born in the 1500s',
40+
inventors.filter(inventor => inventor.year < 1600 && inventor.year > 1499)
41+
)
3642

3743
// Array.prototype.map()
3844
// 2. Give us an array of the inventors' first and last names
45+
console.log(
46+
'inventors first and last names',
47+
inventors.map(inventor => `${inventor.first} ${inventor.last}`)
48+
)
3949

4050
// Array.prototype.sort()
4151
// 3. Sort the inventors by birthdate, oldest to youngest
52+
console.log(
53+
'inventors sorted oldest to youngest',
54+
inventors.sort((inventorA, inventorB) => inventorA.year - inventorB.year)
55+
)
56+
57+
const getLifeSpanOfPerson = ({ passed, year }) => passed - year
4258

4359
// Array.prototype.reduce()
4460
// 4. How many years did all the inventors live?
61+
console.log(
62+
'years all the inventors lived',
63+
inventors.reduce((accumulator, inventor) => accumulator + getLifeSpanOfPerson(inventor), 0)
64+
)
4565

4666
// 5. Sort the inventors by years lived
67+
console.log(
68+
'inventors by years lived',
69+
inventors.sort((a, b) => getLifeSpanOfPerson(b) - getLifeSpanOfPerson(a))
70+
)
4771

4872
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4973
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
5074

5175

5276
// 7. sort Exercise
5377
// Sort the people alphabetically by last name
78+
const getLastNameOfPerson = person => person.match(/\w+/)[0]
79+
80+
console.log(
81+
'sorted people by last name',
82+
people.sort((personA, personB) => getLastNameOfPerson(personA) > getLastNameOfPerson(personB))
83+
)
5484

5585
// 8. Reduce Exercise
5686
// Sum up the instances of each of these
5787
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
88+
console.log(
89+
'instances of words',
90+
data.reduce((accumulator, datum) => {
91+
accumulator[datum] || (accumulator[datum] = 0)
92+
accumulator[datum] += 1
93+
return accumulator
94+
},
95+
{})
96+
)
5897

5998
</script>
6099
</body>

0 commit comments

Comments
 (0)