Skip to content

Commit fcd8e3c

Browse files
committed
day 4 challenge complete
1 parent 90c75ee commit fcd8e3c

File tree

3 files changed

+50
-105
lines changed

3 files changed

+50
-105
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Exercise 4: Array Cardio
2+
Nitish Dayal, Software & Applications Developer - [Contact](http://nitishdayal.me)
3+
Last Commit Date: Dec 10, 2016
4+
5+
Not really much of a guide necessary for this one. The challenge is pretty
6+
well documented as far as what's expected from the developer; use
7+
built-in array methods and perform the necessary operations to provide
8+
the expected values. Easy peasy.
9+
10+
If you're unfamiliar with JavaScript array methods, Mozilla docs are your
11+
friend.
12+
13+
Woooooooo all done with day 4!

exercises/04 - Array Cardio Day 1/index-FINISHED.html

Lines changed: 0 additions & 100 deletions
This file was deleted.

exercises/04 - Array Cardio Day 1/index-START.html renamed to exercises/04 - Array Cardio Day 1/index.html

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>Array Cardio 💪</title>
67
</head>
8+
79
<body>
810
<script>
911
// Get your shorts on - this is an array workout!
@@ -27,29 +29,59 @@
2729

2830
// Array.prototype.filter()
2931
// 1. Filter the list of inventors for those who were born in the 1500's
32+
const inventors_from_1500s = inventors.filter((inventor) => inventor.year >= 1500 && inventor.year < 1600)
33+
console.table(inventors_from_1500s)
3034

3135
// Array.prototype.map()
32-
// 2. Give us an array of the inventory first and last names
36+
// 2. Give us an array of the inventors first and last names
37+
const inventors_full_name = inventors.map((inventor) => `${inventor.first} ${inventor.last}`)
38+
console.table(inventors_full_name)
3339

3440
// Array.prototype.sort()
3541
// 3. Sort the inventors by birthdate, oldest to youngest
42+
const inventors_by_date = inventors.sort((a, b) => a.year - b.year)
43+
console.table(inventors_by_date)
3644

3745
// Array.prototype.reduce()
3846
// 4. How many years did all the inventors live?
39-
47+
const total_years_lived = inventors.reduce((currValue, inventor) => currValue += (inventor.passed - inventor.year), 0)
48+
console.log(total_years_lived);
4049
// 5. Sort the inventors by years lived
50+
const inventors_by_lifespan = inventors.sort((a, b) => (a.passed - a.year) - (b.passed - b.year));
51+
console.table(inventors_by_lifespan);
4152

4253
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4354
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
44-
55+
/*
56+
This code will be run directly from the browser console
57+
after navigating to the Wikipedia link above.
58+
59+
const blvd_with_de = Array.from(document.querySelectorAll('.mw-category a'))
60+
.map(link => link.textContent)
61+
.filter((blvd) => blvd.includes('de'))
62+
*/
4563

4664
// 7. sort Exercise
4765
// Sort the people alphabetically by last name
66+
const people_by_last_name = people.sort(
67+
(a, b) => a.split(', ')[0] > b.split(', ')[0] ? -1 : 1);
4868

69+
console.table(people_by_last_name);
4970
// 8. Reduce Exercise
5071
// Sum up the instances of each of these
51-
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
72+
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck'];
73+
74+
const data_instance_count = data.reduce((tallyObject, item) => {
75+
if (!tallyObject[item]) {
76+
tallyObject[item] = 0;
77+
}
78+
tallyObject[item]++;
79+
return tallyObject;
80+
}, {});
81+
82+
console.table(data_instance_count);
5283

5384
</script>
5485
</body>
55-
</html>
86+
87+
</html>

0 commit comments

Comments
 (0)