|
| 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 | +] |
| 10 | + |
| 11 | +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'] |
| 12 | + |
| 13 | +// 1. Filter the list of inventors for those who were born in the 1500's |
| 14 | +const filteredInventors = inventors.filter(inventor => { |
| 15 | + return inventor.year > 1499 && inventor.year < 1600 |
| 16 | +}) |
| 17 | + |
| 18 | +// 2. Give us an array of the inventory first and last names |
| 19 | +const inventorNames = inventors.map(inventor => { |
| 20 | + return inventor.first + ' ' + inventor.last |
| 21 | +}) |
| 22 | + |
| 23 | +// 3. Sort the inventors by birthdate, oldest to youngest |
| 24 | +const sortedInventors = inventors.sort((a, b) => { |
| 25 | + return a.year > b.year ? 1 : -1 |
| 26 | +}) |
| 27 | + |
| 28 | +// 4. How many years did all the inventors live? |
| 29 | +const totalYearsLived = inventors.reduce((total, currentInventor) => { |
| 30 | + return total + (currentInventor.passed - currentInventor.year) |
| 31 | +}, 0) |
| 32 | + |
| 33 | +// 5. Sort the inventors by years lived |
| 34 | +const sortedYearsLived = inventors.sort((a, b) => { |
| 35 | + return (a.passed - a.year) > (b.passed - a.year) ? -1 : 1 |
| 36 | +}) |
| 37 | + |
| 38 | +// 6. Create a list of Boulevards in Paris that contain 'de' anywhere in the name |
| 39 | +// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 40 | +// const catergory = document.querySelector('.mw-category') |
| 41 | +// const links = Array.from(category.querySelectorAll('a')) |
| 42 | +// |
| 43 | +// const containsDe = links.map(link => { |
| 44 | +// return link.textContent |
| 45 | +// }).filter(streetName => streetName.includes('de')) |
| 46 | + |
| 47 | +// 7. Sort the people alphabetically by last name |
| 48 | +const alphabeticalLast = people.sort((prev, curr) => { |
| 49 | + const [prevLast, prevFirst] = prev.split(', ') |
| 50 | + const [currLast, currFirst] = curr.split(', ') |
| 51 | + return prevLast > currLast ? 1 : -1 |
| 52 | +}) |
| 53 | + |
| 54 | +// 8. Sum up the instances of each of these |
| 55 | +const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck'] |
| 56 | +const talliedData = data.reduce((obj, item) => { |
| 57 | + if (obj[item]) { |
| 58 | + obj[item]++ |
| 59 | + } else { |
| 60 | + obj[item] = 1 |
| 61 | + } |
| 62 | + return obj |
| 63 | +}, {}) |
0 commit comments