Skip to content

Commit cb91aa3

Browse files
author
Brad Stayte
committed
finished 4
1 parent f73ea74 commit cb91aa3

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,65 @@
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+
const name = inventor => `${inventor.first} ${inventor.last}`;
35+
const nameAndYear = inventor => `${name(inventor)} : ${inventor.year}`;
36+
const nameYearAndAge = inventor => `${nameAndYear(inventor)} : ${inventor.passed - inventor.year}`;
37+
const printArray = (results, format) => console.log(results.map(format).join('\n'));
38+
3439
// Array.prototype.filter()
3540
// 1. Filter the list of inventors for those who were born in the 1500's
41+
42+
printArray(inventors.filter(inventor => inventor.year >=1500 && inventor.year < 1600), nameAndYear);
3643

3744
// Array.prototype.map()
3845
// 2. Give us an array of the inventors' first and last names
3946

47+
printArray(inventors, name);
48+
4049
// Array.prototype.sort()
4150
// 3. Sort the inventors by birthdate, oldest to youngest
4251

52+
printArray(inventors.sort((a, b) => a.year - b.year), nameAndYear);
53+
4354
// Array.prototype.reduce()
4455
// 4. How many years did all the inventors live?
4556

57+
console.log(inventors.reduce((sum, curr) => sum + curr.passed - curr.year, 0));
58+
4659
// 5. Sort the inventors by years lived
4760

61+
printArray(inventors.sort((a, b) => (a.passed - a.year) - (b.passed - b.year)), nameYearAndAge);
62+
4863
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4964
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
5065

66+
const titles = [];
67+
document.querySelectorAll('.mw-category-group a').forEach(i => titles.push(i.title));
68+
console.log(titles.filter(i => i.indexOf(' de ') > -1).length);
5169

5270
// 7. sort Exercise
5371
// Sort the people alphabetically by last name
5472

73+
printArray(people.sort((a, b) => {
74+
if (a.toUpperCase() < b.toUpperCase()) {
75+
return -1;
76+
}
77+
if (a.toUpperCase() > b.toUpperCase()) {
78+
return 1;
79+
}
80+
return 0;
81+
}), i => i );
82+
5583
// 8. Reduce Exercise
5684
// Sum up the instances of each of these
5785
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
5886

87+
console.log(data.reduce((all, current) => {
88+
let counter = all.get(current) || 0;
89+
all.set(current, counter+1);
90+
return all;
91+
}, new Map()));
92+
5993
</script>
6094
</body>
6195
</html>

0 commit comments

Comments
 (0)