Visitar URL original
Complete · wesbos/JavaScript30@3f8f8ab · GitHub
Skip to content

Commit 3f8f8ab

Browse files
committed
Complete
1 parent 02dc1ce commit 3f8f8ab

2 files changed

Lines changed: 76 additions & 107 deletions

File tree

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

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

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

Lines changed: 76 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
<!DOCTYPE html>
2-
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8">
5-
<title>Array Cardio 💪</title>
6-
</head>
7-
<body>
8-
<p><em>Psst: have a look at the JavaScript Console</em> 💁</p>
9-
<script>
101
// Get your shorts on - this is an array workout!
112
// ## Array Cardio Day 1
123

@@ -31,65 +22,106 @@
3122

3223
// Array.prototype.filter()
3324
// 1. Filter the list of inventors for those who were born in the 1500's
34-
const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600));
3525

36-
console.table(fifteen);
26+
const fifteenHundreds = inventors.filter(function (inventor) {
27+
if (inventor.year >= 1500 && inventor.year < 1600) {
28+
return true; // By returning true it means we keep it.
29+
};
30+
});
31+
32+
// This example also works
33+
// const fifteenHundreds = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600));
34+
35+
console.log(fifteenHundreds);
3736

3837
// Array.prototype.map()
39-
// 2. Give us an array of the inventor first and last names
40-
const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`);
41-
console.log(fullNames);
38+
// 2. Give us an array of the inventors' first and last names
39+
40+
// My solution.
41+
const fullName = inventors.map(function (inventor) {
42+
return inventor.first + ' ' + inventor.last;
43+
});
44+
45+
//His solution using concatinating stings (ES6)
46+
// const fullName = inventors.map(inventor => `${inventor.first} ${inventor.last}`);
47+
48+
console.log(fullName);
4249

4350
// Array.prototype.sort()
4451
// 3. Sort the inventors by birthdate, oldest to youngest
45-
// const ordered = inventors.sort(function(a, b) {
46-
// if(a.year > b.year) {
47-
// return 1;
48-
// } else {
49-
// return -1;
50-
// }
51-
// });
5252

53-
const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1);
54-
console.table(ordered);
53+
// My Solution
54+
const oldestToYoungest = inventors.sort(function (a ,b) {
55+
return a.year - b.year;
56+
});
57+
58+
// His solution
59+
// const oldestToYoungest = inventors.sort(function (a ,b) {
60+
// if (a.year > b.year) {
61+
//
62+
// return 1;
63+
//
64+
// }else{
65+
//
66+
// return -1;
67+
// }
68+
// });
69+
70+
console.log(oldestToYoungest);
71+
5572

5673
// Array.prototype.reduce()
5774
// 4. How many years did all the inventors live?
58-
const totalYears = inventors.reduce((total, inventor) => {
59-
return total + (inventor.passed - inventor.year);
60-
}, 0);
6175

62-
console.log(totalYears);
76+
// His Solution. Not to sure how this one worked
77+
const totalYears = inventors.reduce((total, inventor) => {
78+
return total + (inventor.passed - inventor.year)
79+
}, 0);
80+
81+
console.log('Total Years: ' + totalYears);
6382

6483
// 5. Sort the inventors by years lived
65-
const oldest = inventors.sort(function(a, b) {
66-
const lastInventor = a.passed - a.year;
67-
const nextInventor = b.passed - b.year;
68-
return lastInventor > nextInventor ? -1 : 1;
84+
85+
//His solution
86+
const yearsLived = inventors.sort(function (a ,b) {
87+
const firstGuy = a.passed - a.year;
88+
const nextGuy = b.passed - b.year;
89+
return firstGuy > nextGuy ? -1 : 1;
6990
});
70-
console.table(oldest);
91+
92+
console.log(yearsLived);
7193

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

75-
// const category = document.querySelector('.mw-category');
76-
// const links = Array.from(category.querySelectorAll('a'));
77-
// const de = links
78-
// .map(link => link.textContent)
79-
// .filter(streetName => streetName.includes('de'));
97+
// His solution
98+
// First you must look at the HTML doc (dev tools) and find what you need to select.
99+
// const category = document.querySelector('.mw-category');
100+
101+
// You can call querySelector against any existing DOM element.
102+
// You also need to turn the links variable into an array so .map is accessable in devtools.
103+
// const links = Array.from(category.querySelectorAll('a'));
104+
//
105+
// const de = links
106+
// .map(link => link.textContent)
107+
// .filter(streetName => streetName.includes('de'));
80108

81109
// 7. sort Exercise
82110
// Sort the people alphabetically by last name
83-
const alpha = people.sort((lastOne, nextOne) => {
84-
const [aLast, aFirst] = lastOne.split(', ');
85-
const [bLast, bFirst] = nextOne.split(', ');
86-
return aLast > bLast ? 1 : -1;
87-
});
88-
console.log(alpha);
111+
112+
// Currently not working.
113+
// const alpha = people.sort(function (lastOne, nextOne) {
114+
// const [ aLast, aFirst] = lastOne.split(', ');
115+
// const [ bLast, bFirst] = lastOne.split(', ');
116+
// return aLast > bLast ? 1 : -1;
117+
// });
118+
//
119+
// console.log(alpha);
89120

90121
// 8. Reduce Exercise
91122
// Sum up the instances of each of these
92-
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck', 'pogostick'];
123+
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
124+
93125

94126
const transportation = data.reduce(function(obj, item) {
95127
if (!obj[item]) {
@@ -100,7 +132,3 @@
100132
}, {});
101133

102134
console.log(transportation);
103-
104-
</script>
105-
</body>
106-
</html>

0 commit comments

Comments
 (0)