Visitar URL original
finished array exercises except #6 · lilyhoratio/JavaScript30@ed0b1d7 · GitHub
Skip to content

Commit ed0b1d7

Browse files
committed
finished array exercises except wesbos#6
1 parent 360851d commit ed0b1d7

1 file changed

Lines changed: 37 additions & 5 deletions

File tree

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

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
// Get your shorts on - this is an array workout!
1111
// ## Array Cardio Day 1
1212

13-
// Some data we can work with
14-
1513
const inventors = [
1614
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
1715
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
@@ -31,28 +29,62 @@
3129

3230
// Array.prototype.filter()
3331
// 1. Filter the list of inventors for those who were born in the 1500's
34-
32+
const inventorsOld = inventors.filter(inventor => inventor.year >= 1500 && inventor.year <1600);
33+
console.log(inventorsOld);
34+
console.table(inventorsOld);
3535
// Array.prototype.map()
3636
// 2. Give us an array of the inventors' first and last names
37-
37+
const firstAndLast = inventors.map(inventor => `${inventor.first} ${inventor.last}`)
38+
console.log(firstAndLast)
3839
// Array.prototype.sort()
3940
// 3. Sort the inventors by birthdate, oldest to youngest
41+
inventors.sort((a,b) => a.year - b.year)
42+
inventors.sort((a,b) => a.year > b.year ? 1 : -1);
43+
console.table(inventors)
4044

4145
// Array.prototype.reduce()
4246
// 4. How many years did all the inventors live?
47+
const totalYears = inventors.reduce((acc, inventor) => { return acc + (inventor.passed - inventor.year)},0)
48+
console.log("total years:", totalYears);
4349

4450
// 5. Sort the inventors by years lived
51+
const yearsLivedSorted = inventors.sort((a,b) => {
52+
// console.log(a);
53+
// console.log("----------");
54+
// console.log(b);
55+
// console.log("NEXT------");
56+
const firstYears = a.passed - a.year;
57+
const nextYears = b.passed - b.year;
58+
return (firstYears > nextYears) ? -1 : 1
59+
// (a.passed - a.year)-(b.passed - b.year)
60+
})
61+
console.table(yearsLivedSorted);
4562

4663
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4764
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
48-
65+
4966

5067
// 7. sort Exercise
5168
// Sort the people alphabetically by last name
69+
// const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black, Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William'];
70+
people.sort((a,b)=> a[0] - b[0]) // ascending
71+
console.log(people);
5272

5373
// 8. Reduce Exercise
5474
// Sum up the instances of each of these
5575
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
76+
77+
const summedData = data.reduce((object, item) => {
78+
if (object[item] == undefined) {
79+
// if (!object[item]) {
80+
object[item] = 1;
81+
} else {
82+
object[item]++;
83+
}
84+
return object;
85+
}, {})
86+
87+
console.log(summedData);
5688

5789
</script>
5890
</body>

0 commit comments

Comments
 (0)