Skip to content

Commit b0cda3a

Browse files
committed
Day 4: Javascript Arrays
1 parent bfe2755 commit b0cda3a

1 file changed

Lines changed: 47 additions & 1 deletion

File tree

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,74 @@
3131

3232
// Array.prototype.filter()
3333
// 1. Filter the list of inventors for those who were born in the 1500's
34+
function checkFifteen(inventor) {
35+
return (inventor.year >= 1500 && inventor.year < 1600);
36+
}
37+
const fifteen = inventors.filter(checkFifteen);
38+
console.log(fifteen);
3439

3540
// Array.prototype.map()
3641
// 2. Give us an array of the inventors' first and last names
42+
function mapName(inventor) {
43+
return inventor.first + " " + inventor.last;
44+
}
45+
const inventorNames = inventors.map(mapName);
46+
console.log(inventorNames);
3747

3848
// Array.prototype.sort()
3949
// 3. Sort the inventors by birthdate, oldest to youngest
50+
function compareBirth(a, b) {
51+
return a.year - b.year;
52+
}
53+
const sortedBirth = inventors.sort(compareBirth);
54+
console.log(sortedBirth);
4055

4156
// Array.prototype.reduce()
4257
// 4. How many years did all the inventors live?
58+
function reduceYears(sum, inventor) {
59+
return sum + (inventor.passed - inventor.year);
60+
}
61+
const totalYears = inventors.reduce(reduceYears, 0);
62+
console.log(totalYears);
4363

4464
// 5. Sort the inventors by years lived
65+
function compareYears(a, b) {
66+
return (a.passed - a.year) - (b.passed - b.year);
67+
}
68+
const yearsLived = inventors.sort(compareYears);
69+
console.log(yearsLived);
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
48-
73+
// const wrapper = document.querySelector(".mw-category");
74+
// const links = Array.prototype.slice.call(wrapper.querySelectorAll("a"));
75+
// const boulevardNames = links.map(link => link.textContent);
76+
// const de = boulevardNames.filter(name => name.includes("de"));
77+
// console.log(de);
4978

5079
// 7. sort Exercise
5180
// Sort the people alphabetically by last name
81+
function compareLast(a, b) {
82+
const lasta = a.split(", ")[0];
83+
const lastb = b.split(", ")[0];
84+
return lasta.localeCompare(lastb);
85+
}
86+
const sortedLast = people.sort(compareLast);
87+
console.log(sortedLast);
5288

5389
// 8. Reduce Exercise
5490
// Sum up the instances of each of these
5591
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
92+
function sumWords(obj, word) {
93+
if (obj[word] == null) {
94+
obj[word] = 1;
95+
} else {
96+
obj[word] += 1;
97+
}
98+
return obj;
99+
}
100+
const sumInstances = data.reduce(sumWords, {});
101+
console.log(sumInstances);
56102

57103
</script>
58104
</body>

0 commit comments

Comments
 (0)