Skip to content

Commit 309f7d0

Browse files
committed
add answer to some quiz
1 parent 11e551d commit 309f7d0

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,84 @@
3131

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

3541
// Array.prototype.map()
3642
// 2. Give us an array of the inventors first and last names
43+
console.log('#2. Map');
44+
console.log(
45+
inventors.map( inventor => {
46+
return {
47+
first: inventor.first,
48+
last: inventor.last
49+
}
50+
})
51+
);
3752

3853
// Array.prototype.sort()
3954
// 3. Sort the inventors by birthdate, oldest to youngest
55+
console.log('#3. Sort');
56+
console.log(
57+
inventors.sort( (a, b) => {
58+
if (a.year > b.year) {
59+
return 1;
60+
} else if (a.year < b.year) {
61+
return -1;
62+
} else {
63+
return 0;
64+
}
65+
})
66+
);
4067

4168
// Array.prototype.reduce()
4269
// 4. How many years did all the inventors live all together?
70+
console.log('#4. Reduce');
71+
console.log(
72+
inventors.reduce((sum, inventor) => {
73+
console.log(sum, inventor);
74+
return (sum + (inventor.passed - inventor.year));
75+
}, 0)
76+
)
4377

4478
// 5. Sort the inventors by years lived
79+
console.log('#5. Sort 2');
80+
console.log(
81+
inventors.sort( (a, b) => {
82+
if ((a.passed - a.year) > (b.passed - b.year)) {
83+
return 1;
84+
} else if ((a.passed - a.year) < (b.passed - b.year)) {
85+
return -1;
86+
} else {
87+
return 0;
88+
}
89+
})
90+
)
4591

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

4995

5096
// 7. sort Exercise
5197
// Sort the people alphabetically by last name
98+
console.log('#7. Sort Last Name');
99+
console.log(
100+
people.sort((a, b) => {
101+
let a_lastName = a.split(', ')[1];
102+
let b_lastName = b.split(', ')[1];
103+
if (a_lastName > b_lastName) {
104+
return 1;
105+
} else if (a_lastName < b_lastName) {
106+
return -1;
107+
} else {
108+
return 0;
109+
}
110+
})
111+
);
52112

53113
// 8. Reduce Exercise
54114
// Sum up the instances of each of these

0 commit comments

Comments
 (0)