|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Array Cardio 💪</title> |
| 6 | + <script src="http://localhost:35729/livereload.js"></script> |
| 7 | +</head> |
| 8 | +<body> |
| 9 | + <script> |
| 10 | + // Get your shorts on - this is an array workout! |
| 11 | + // ## Array Cardio Day 1 |
| 12 | + |
| 13 | + // Some data we can work with |
| 14 | + |
| 15 | + const inventors = [ |
| 16 | + { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 }, |
| 17 | + { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 }, |
| 18 | + { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 }, |
| 19 | + { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 }, |
| 20 | + { first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 }, |
| 21 | + { first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 }, |
| 22 | + { first: 'Max', last: 'Planck', year: 1858, passed: 1947 } |
| 23 | + ]; |
| 24 | + |
| 25 | + 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']; |
| 26 | + |
| 27 | + const people = ['Blake, William', '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']; |
| 28 | + |
| 29 | + // Array.prototype.filter() |
| 30 | + // 1. Filter the list of inventors for those who were born in the 1500's |
| 31 | + const oldies = inventors.filter(elm => elm.year < 1600 && elm.year >= 1500); |
| 32 | + console.table(oldies); |
| 33 | + |
| 34 | + // Array.prototype.map() |
| 35 | + // 2. Give us an array of the inventory first and last names |
| 36 | + const names = inventors.map(elm => elm.first + ' ' + elm.last); |
| 37 | + console.log(names); |
| 38 | + |
| 39 | + // Array.prototype.sort() |
| 40 | + // 3. Sort the inventors by birthdate, oldest to youngest |
| 41 | + const sorted = inventors.sort((a, b) => a.year > b.year); |
| 42 | + console.table(sorted); |
| 43 | + |
| 44 | + // Array.prototype.reduce() |
| 45 | + // 4. How many years did all the inventors live? |
| 46 | + const livespan = inventors.reduce((p, c) => p + (c.passed - c.year), 0); |
| 47 | + console.table(livespan) |
| 48 | + |
| 49 | + // 5. Sort the inventors by years lived |
| 50 | + const sortedAlive = inventors |
| 51 | + .map(elm => Object.assign(elm, { livespan: elm.passed - elm.year })) |
| 52 | + .sort((a,b) => a.livespan < b.livespan); |
| 53 | + console.table(sortedAlive); |
| 54 | + |
| 55 | + // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
| 56 | + // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 57 | + // > should be run on the wikipedia console |
| 58 | + const blvds = Array.from(document.querySelectorAll('.mw-category a')) |
| 59 | + .map(elm => elm.textContent) |
| 60 | + .filter(elm => elm.indexOf('de') > -1); |
| 61 | + |
| 62 | + // 7. sort Exercise |
| 63 | + // Sort the people alphabetically by last name |
| 64 | + const sortedLast = people.sort((a,b) => { |
| 65 | + const aLast = a.split(', ')[0]; |
| 66 | + const bLast = b.split(', ')[0]; |
| 67 | + return aLast > bLast ? 1 : -1; |
| 68 | + }); |
| 69 | + console.table(sortedLast); |
| 70 | + |
| 71 | + // 8. Reduce Exercise |
| 72 | + // Sum up the instances of each of these |
| 73 | + const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 74 | + const cnt = data.reduce((p, c) => { |
| 75 | + if (!p[c]) { |
| 76 | + p[c] = 0; |
| 77 | + } |
| 78 | + p[c]++; |
| 79 | + return p; |
| 80 | + }, {}); |
| 81 | + console.table(cnt); |
| 82 | + </script> |
| 83 | +</body> |
| 84 | +</html> |
0 commit comments