Skip to content

Commit 671f2a9

Browse files
committed
Completed exercise wesbos#4 of JS30.
1 parent 9ed8eb1 commit 671f2a9

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,60 @@
3131

3232
// Array.prototype.filter()
3333
// 1. Filter the list of inventors for those who were born in the 1500's
34-
34+
const fifteenHundreds = inventors.filter(x => x.year >= 1500 && x.year < 1600);
35+
console.log(fifteenHundreds);
36+
3537
// Array.prototype.map()
3638
// 2. Give us an array of the inventors' first and last names
39+
const names = inventors.map(x => x.first + ` ` + x.last);
40+
console.table(names);
3741

3842
// Array.prototype.sort()
3943
// 3. Sort the inventors by birthdate, oldest to youngest
44+
const sorted = inventors.sort((x, y) => x.year > y.year ? 1 : -1);
45+
console.table (sorted);
4046

4147
// Array.prototype.reduce()
4248
// 4. How many years did all the inventors live?
49+
const objAge = inventors.reduce((obj, x) => obj + x.passed - x.year, 0);
50+
console.log(objAge);
4351

4452
// 5. Sort the inventors by years lived
53+
const ages = inventors.map(x => x.passed - x.year);
54+
const sortedByAge = inventors.sort((x,y) => x.passed - x.year > y.passed - y.year ? 1 : -1);
55+
console.log(ages);
56+
console.table(sortedByAge);
4557

4658
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4759
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
48-
60+
// const categories = document.querySelector(`.mw-category`);
61+
// const links = Array.from(categories.querySelectorAll(`a`));
62+
// const de = links
63+
// .map(x => x.text)
64+
// .filter(x => x.indexOf(`de`) >= 0);
4965

5066
// 7. sort Exercise
5167
// Sort the people alphabetically by last name
68+
const sortedPeople = people.sort((a, b) => {
69+
const [aFirst, aLast] = a.split(', ');
70+
const [bFirst, bLast] = b.split(', ');
71+
return aLast > bLast ? 1 : -1;
72+
}).map(x => x.split(', ')[1] + ' ' + x.split(', ')[0]);
73+
console.log(sortedPeople);
5274

5375
// 8. Reduce Exercise
5476
// Sum up the instances of each of these
5577
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
78+
const instances = data
79+
.reduce((obj, next) => {
80+
if (!obj[next])
81+
{
82+
obj[next] = 0;
83+
}
84+
obj[next]++;
85+
return obj;
86+
}, {});
87+
console.log(instances);
5688

5789
</script>
5890
</body>

0 commit comments

Comments
 (0)