|
| 1 | +const inventors = [ |
| 2 | + { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 }, |
| 3 | + { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 }, |
| 4 | + { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 }, |
| 5 | + { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 }, |
| 6 | + { first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 }, |
| 7 | + { first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 }, |
| 8 | + { first: 'Max', last: 'Planck', year: 1858, passed: 1947 }, |
| 9 | + { first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 }, |
| 10 | + { first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 }, |
| 11 | + { first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 }, |
| 12 | + { first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 }, |
| 13 | + { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 } |
| 14 | +]; |
| 15 | + |
| 16 | +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']; |
| 17 | + |
| 18 | +// Array.prototype.filter() |
| 19 | +// 1. Filter the list of inventors for those who were born in the 1500's |
| 20 | +let myInventors1500 = inventors.filter(function(inventor) { |
| 21 | + if (inventor.year >= 1500 && inventor.year < 1600) { |
| 22 | + return true; |
| 23 | + } |
| 24 | +}); |
| 25 | + |
| 26 | +console.log(myInventors1500); |
| 27 | +console.log('--------'); |
| 28 | + |
| 29 | +// Array.prototype.map() |
| 30 | +// 2. Give us an array of the inventors' first and last names |
| 31 | +let myInventorsNames = inventors.map(function(inventor) { |
| 32 | + return `${inventor.first} ${inventor.last}`; |
| 33 | +}); |
| 34 | + |
| 35 | +console.log(myInventorsNames); |
| 36 | +console.log('--------'); |
| 37 | + |
| 38 | +// Array.prototype.sort() |
| 39 | +// 3. Sort the inventors by birthdate, oldest to youngest |
| 40 | +let sortedInventors = inventors.sort(function(inventorOne, inventorTwo) { |
| 41 | + return inventorOne.year > inventorTwo.year ? 1 : -1; |
| 42 | +}); |
| 43 | + |
| 44 | +console.log(sortedInventors); |
| 45 | +console.log('--------'); |
| 46 | + |
| 47 | +// Array.prototype.reduce() |
| 48 | +// 4. How many years did all the inventors live? |
| 49 | +let totalYears = inventors.reduce(function(total, inventor) { |
| 50 | + return total + (inventor.passed - inventor.year); |
| 51 | +}, 0); |
| 52 | + |
| 53 | +console.log(totalYears); |
| 54 | +console.log('--------'); |
| 55 | + |
| 56 | +// 5. Sort the inventors by years lived |
| 57 | +let sortedInventorsOldest = inventors.sort(function(inventorOne, inventorTwo) { |
| 58 | + return inventorOne.passed - inventorOne.year > inventorTwo.passed - inventorTwo.year ? -1 : 1; |
| 59 | +}); |
| 60 | + |
| 61 | +console.log(sortedInventorsOldest); |
| 62 | +console.log('--------'); |
| 63 | + |
| 64 | +// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
| 65 | +// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 66 | +// const category = document.querySelector('.mw-category'); |
| 67 | +// const links = Array.from(category.querySelectorAll('a')); |
| 68 | +// const de = links |
| 69 | +// .map(link => link.textContent) |
| 70 | +// .filter(streetName => streetName.includes('de')); |
| 71 | + |
| 72 | + |
| 73 | +// 7. sort Exercise |
| 74 | +// Sort the people alphabetically by last name |
| 75 | +let sortedByLastName = people.sort(function(personOne, personTwo) { |
| 76 | + let [lastNameOne, firstNameOne] = personOne.split(', '); |
| 77 | + let [lastNameTwo, firstNameTwo] = personTwo.split(','); |
| 78 | + |
| 79 | + |
| 80 | + return lastNameOne > lastNameTwo? 1 : -1; |
| 81 | +}); |
| 82 | + |
| 83 | +console.log(sortedByLastName); |
| 84 | +console.log('--------'); |
| 85 | + |
| 86 | +// 8. Reduce Exercise |
| 87 | +// Sum up the instances of each of these |
| 88 | +let data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 89 | +let totalPerVehicle = data.reduce(function(obj, vehicle) { |
| 90 | + if (!obj[vehicle]) { |
| 91 | + obj[vehicle] = 0; |
| 92 | + } |
| 93 | + |
| 94 | + obj[vehicle]++ |
| 95 | + |
| 96 | + return obj; |
| 97 | + |
| 98 | +}, {}); |
| 99 | + |
| 100 | +console.log(totalPerVehicle); |
| 101 | + |
| 102 | + |
0 commit comments