Visitar URL original
Merge pull request #4 from cydseelen/cardio · wesbos/JavaScript30@d5f9347 · GitHub
Skip to content

Commit d5f9347

Browse files
authored
Merge pull request #4 from cydseelen/cardio
Completed cardio challenge. Will repeat until confident with the exercises.
2 parents 6a4e6f2 + 0c76c69 commit d5f9347

1 file changed

Lines changed: 51 additions & 3 deletions

File tree

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

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// ## Array Cardio Day 1
1212

1313
// Some data we can work with
14+
// REPEAT THSES UNTIL CONFIDENT AND ABLE TO GO TO A WEBSITE AND REPLICATE
1415

1516
const inventors = [
1617
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
@@ -34,32 +35,79 @@
3435
'Berne, Eric', 'Berra, Yogi', 'Berry, Wendell', 'Bevan, Aneurin', 'Ben-Gurion, David', 'Bevel, Ken', 'Biden, Joseph', 'Bennington, Chester', 'Bierce, Ambrose',
3536
'Billings, Josh', 'Birrell, Augustine', 'Blair, Tony', 'Beecher, Henry', 'Biondo, Frank'
3637
];
37-
38+
3839
// Array.prototype.filter()
3940
// 1. Filter the list of inventors for those who were born in the 1500's
41+
const fifteen = inventors.filter(function(inventor) {
42+
if(inventor.year >= 1500 && inventor.year < 1600) {
43+
return true; //keeps the ones that are true
44+
}
45+
});
46+
console.table(fifteen);
4047

4148
// Array.prototype.map()
4249
// 2. Give us an array of the inventors first and last names
50+
const fullNames = inventors.map(inventor => inventor.first + ' ' + inventor.last);
51+
console.log(fullNames);
52+
// another way to do it const fullNames = inventors.map(inventor => `${inventor.first}` `$inventor.last`);
4353

4454
// Array.prototype.sort()
4555
// 3. Sort the inventors by birthdate, oldest to youngest
56+
// const ordered = inventors.sort(function(firstPerson, secondPerson) {
57+
// if(firstPerson.year > secondPerson.year) {
58+
// return 1;
59+
// } else {
60+
// return -1;
61+
// }
62+
// });
63+
const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1);
64+
65+
console.table(ordered);
4666

4767
// Array.prototype.reduce()
4868
// 4. How many years did all the inventors live all together?
69+
const totalYears = inventors.reduce((total, inventor) => {
70+
return total + (inventor.passed - inventor.year);
71+
}, 0);
72+
console.log(totalYears);
4973

5074
// 5. Sort the inventors by years lived
51-
75+
const oldest = inventors.sort(function(a, b) {
76+
const lastGuy = a.passed - a.year;
77+
const nextGuy = b.passed - b.year;
78+
return lastGuy > nextGuy ? -1 : 1;
79+
});
80+
console.table(oldest);
5281
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
5382
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
54-
83+
//code run in the browser
84+
// const category = document.querySelector('.mw-category');
85+
// const links = Array.from(category.querySelectorAll('a'));
86+
// const de = links
87+
// .map(link => link.textContent)
88+
// .filter(streetName => streetName.includes('de'));
5589

5690
// 7. sort Exercise
5791
// Sort the people alphabetically by last name
92+
const alpha = people.sort(function(lastOne, nextOne) {
93+
const [aLast, aFirst] = lastOne.split(', ');
94+
const [bLast, bFirst] = nextOne.split(', ');
95+
return aLast > bLast ? 1 : -1;
96+
});
97+
console.log(alpha);
5898

5999
// 8. Reduce Exercise
60100
// Sum up the instances of each of these
61101
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
62102

103+
const transportation = data.reduce(function(obj, item) {
104+
if(!obj[item]){
105+
obj[item] = 0; //
106+
}
107+
obj[item]++; //iterates here
108+
return obj;
109+
}, {}); //<-- the empty {} are a blank object that we start with. The iterate and count the amount of each object is present in the array data
110+
console.log(transportation);
63111
</script>
64112
</body>
65113
</html>

0 commit comments

Comments
 (0)