Skip to content

Commit 50919f3

Browse files
committed
Day 4 Array Cardio Day 1
What’s this => operator all about? console.table is really useful Let’s understand map and reduce functions How can I get better code highlighting in chrome dev tools, word wrap in JS sources (no) Why use const instead of var?
1 parent 28808b9 commit 50919f3

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,77 @@
2929

3030
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'];
3131

32-
// Array.prototype.filter()
32+
// Array.prototype.filter()d
3333
// 1. Filter the list of inventors for those who were born in the 1500's
34+
const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600))
35+
console.table(fifteen);
3436

3537
// Array.prototype.map()
3638
// 2. Give us an array of the inventors' first and last names
39+
const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`);
40+
console.log(fullNames);
3741

3842
// Array.prototype.sort()
3943
// 3. Sort the inventors by birthdate, oldest to youngest
44+
// const ordered = inventors.sort(function(a, b){
45+
// if(a.year > b.year){
46+
// return 1;
47+
// } else {
48+
// return -1;
49+
// }
50+
// });
51+
52+
const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1);
53+
console.table(ordered);
4054

4155
// Array.prototype.reduce()
4256
// 4. How many years did all the inventors live?
57+
const totalYears = inventors.reduce((total, inventor) => {
58+
return total + (inventor.passed - inventor.year);
59+
}, 0);
60+
console.log(totalYears);
4361

4462
// 5. Sort the inventors by years lived
63+
const oldest = inventors.sort(function(a, b){
64+
const lastGuy = a.passed - a.year;
65+
const nextGuy = b.passed - b.year;
66+
return lastGuy > nextGuy ? -1 : 1;
67+
68+
});
69+
console.table(oldest);
4570

4671
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4772
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
73+
// const category = document.querySelector('.mw-category');
74+
// const links = Array.from(category.querySelectorAll('a'));
75+
// const de = links
76+
// .map(link => link.textContent)
77+
// .filter(streetName => streetName.includes('de'));
4878

4979

5080
// 7. sort Exercise
5181
// Sort the people alphabetically by last name
82+
const alpha = people.sort((lastOne, nextOne) => {
83+
const [aLast, aFirst] = lastOne.split(', ');
84+
const [bLast, bFirst] = nextOne.split(', ');
85+
return aLast > bLast ? 1 : -1;
86+
87+
});
88+
console.log(alpha);
5289

5390
// 8. Reduce Exercise
5491
// Sum up the instances of each of these
5592
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
5693

94+
const transportation = data.reduce(function(obj, item) {
95+
if(!obj[item]) {
96+
obj[item] = 0;
97+
}
98+
obj[item]++;
99+
return obj;
100+
101+
}, {})
102+
console.log(transportation);
57103
</script>
58104
</body>
59105
</html>

0 commit comments

Comments
 (0)