|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Array Cardio 💪</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + <script> |
| 9 | + // Get your shorts on - this is an array workout! |
| 10 | + // ## Array Cardio Day 1 |
| 11 | + |
| 12 | + // Some data we can work with |
| 13 | + |
| 14 | + const inventors = [ |
| 15 | + { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 }, |
| 16 | + { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 }, |
| 17 | + { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 }, |
| 18 | + { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 }, |
| 19 | + { first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 }, |
| 20 | + { first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 }, |
| 21 | + { first: 'Max', last: 'Planck', year: 1858, passed: 1947 } |
| 22 | + ]; |
| 23 | + |
| 24 | + const flavours = ['Chocolate Chip', 'Kulfi', 'Caramel Praline', 'Chocolate', 'Burnt Caramel', 'Pistachio', 'Rose', 'Sweet Coconut', 'Lemon Cookie', 'Toffeeness', 'Toasted Almond', 'Black Raspberry Crunch', 'Chocolate Brownies', 'Pistachio Almond', 'Strawberry', 'Lavender Honey', 'Lychee', 'Peach', 'Black Walnut', 'Birthday Cake', 'Mexican Chocolate', 'Mocha Almond Fudge', 'Raspberry']; |
| 25 | + |
| 26 | + 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']; |
| 27 | + |
| 28 | + // Array.prototype.filter() |
| 29 | + // 1. Filter the list of inventors for those who were born in the 1500's |
| 30 | + const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600)) |
| 31 | + |
| 32 | + console.table(fifteen); |
| 33 | + // Array.prototype.map() |
| 34 | + // 2. Give us an array of the inventory first and last names |
| 35 | + const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`); |
| 36 | + console.log(fullNames); |
| 37 | + // Array.prototype.sort() |
| 38 | + // 3. Sort the inventors by birthdate, oldest to youngest |
| 39 | + const ordered = inventors.sort(function(a, b) { |
| 40 | + if(a.year > b.year) { |
| 41 | + return 1; |
| 42 | + } else { |
| 43 | + return -1; |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + // refactored as |
| 48 | + const ordered2 = inventors.sort((a, b) => a.year > b.year ? 1 : -1); |
| 49 | + |
| 50 | + console.table(ordered); |
| 51 | + console.table(ordered2); |
| 52 | + // Array.prototype.reduce() |
| 53 | + // 4. How many years did all the inventors live? |
| 54 | + const totalYears = inventors.reduce((total, inventor) => { |
| 55 | + return total + (inventor.passed - inventor.year); |
| 56 | + }, 0); |
| 57 | + console.log(totalYears); |
| 58 | + // 5. Sort the inventors by years lived |
| 59 | + const oldest = inventors.sort(function(a, b) { |
| 60 | + const lastGuy = a.passed - a.year; |
| 61 | + const nextGuy = b.passed - b.year; |
| 62 | + return lastGuy > nextGuy ? -1 : 1; |
| 63 | + }); |
| 64 | + console.log(oldest); |
| 65 | + // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
| 66 | + // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 67 | + const category = document.querySelector('.mw-category'); |
| 68 | + const links = category.querySelectorAll('a'); |
| 69 | + |
| 70 | + // 7. sort Exercise |
| 71 | + // Sort the people alphabetically by last name |
| 72 | + |
| 73 | + // 8. Reduce Exercise |
| 74 | + // Sum up the instances of each of these |
| 75 | + const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 76 | + |
| 77 | + </script> |
| 78 | +</body> |
| 79 | +</html> |
0 commit comments