Skip to content

Commit 8e83e79

Browse files
author
Jin Xu
committed
course wesbos#4 finished
1 parent 73de491 commit 8e83e79

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,62 @@
3131

3232
// Array.prototype.filter()
3333
// 1. Filter the list of inventors for those who were born in the 1500's
34-
34+
let result_1 = inventors.filter((person) => person.year >= 1500 && person.year < 1600)
35+
console.table(result_1);
36+
3537
// Array.prototype.map()
3638
// 2. Give us an array of the inventors' first and last names
39+
let result_2 = [];
40+
inventors.map(person => {result_2.push(person.first + ' ' + person.last)})
41+
console.log(result_2);
3742

3843
// Array.prototype.sort()
3944
// 3. Sort the inventors by birthdate, oldest to youngest
45+
inventors.sort((a, b) => (- a.year + b.year))
46+
console.table(inventors);
47+
4048

4149
// Array.prototype.reduce()
4250
// 4. How many years did all the inventors live?
51+
// let total = 0;
52+
// inventors.reduce((prePerson, curPerson, curIdx, src) => {
53+
// total += (curPerson.passed - curPerson.year)
54+
// }, 0)
55+
// console.log(total);
56+
const totalYears = inventors.reduce((total, inventor) => (total + (inventor.passed - inventor.year)), 0);
57+
58+
console.log(totalYears);
4359

4460
// 5. Sort the inventors by years lived
61+
inventors.sort((a, b) => (-(a.passed - a.year) + (b.passed - b.year)))
62+
console.table(inventors);
4563

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

67+
/* run the code in console of the wiki page*/
68+
// var result_6 = [...document.querySelectorAll('.mw-category a')].map(function(i){return i.innerHTML});
69+
// result_6.filter((n) => n.indexOf('de')>=0);
4970

5071
// 7. sort Exercise
5172
// Sort the people alphabetically by last name
73+
people.sort((a,b) => (a.split(',')[0].trim() > b.split(',')[0].trim() ? 1 : -1));
74+
console.log(people)
5275

5376
// 8. Reduce Exercise
5477
// Sum up the instances of each of these
5578
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
56-
79+
var result_8 = data.reduce((obj, item) => {
80+
if(obj.hasOwnProperty(item)) {
81+
obj[item] ++;
82+
}
83+
else {
84+
obj[item] = 0;
85+
}
86+
return obj;
87+
}, {});
88+
console.log(result_8);
89+
5790
</script>
5891
</body>
5992
</html>

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
1. [x] ~~JavaScript Drum Kit~~
88
2. [x] ~~JS + CSS Clock~~
99
3. [x] ~~CSS Variables~~
10-
4. [ ] Array Cardio, Day 1
10+
4. [x] ~~Array Cardio, Day 1~~
1111
5. [ ] Flex Panel Gallery
1212
6. [ ] Type Ahead
1313
7. [ ] Array Cardio, Day 2

0 commit comments

Comments
 (0)