|
31 | 31 |
|
32 | 32 | // Array.prototype.filter() |
33 | 33 | // 1. Filter the list of inventors for those who were born in the 1500's |
| 34 | + function checkFifteen(inventor) { |
| 35 | + return (inventor.year >= 1500 && inventor.year < 1600); |
| 36 | + } |
| 37 | + const fifteen = inventors.filter(checkFifteen); |
| 38 | + console.log(fifteen); |
34 | 39 |
|
35 | 40 | // Array.prototype.map() |
36 | 41 | // 2. Give us an array of the inventors' first and last names |
| 42 | + function mapName(inventor) { |
| 43 | + return inventor.first + " " + inventor.last; |
| 44 | + } |
| 45 | + const inventorNames = inventors.map(mapName); |
| 46 | + console.log(inventorNames); |
37 | 47 |
|
38 | 48 | // Array.prototype.sort() |
39 | 49 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 50 | + function compareBirth(a, b) { |
| 51 | + return a.year - b.year; |
| 52 | + } |
| 53 | + const sortedBirth = inventors.sort(compareBirth); |
| 54 | + console.log(sortedBirth); |
40 | 55 |
|
41 | 56 | // Array.prototype.reduce() |
42 | 57 | // 4. How many years did all the inventors live? |
| 58 | + function reduceYears(sum, inventor) { |
| 59 | + return sum + (inventor.passed - inventor.year); |
| 60 | + } |
| 61 | + const totalYears = inventors.reduce(reduceYears, 0); |
| 62 | + console.log(totalYears); |
43 | 63 |
|
44 | 64 | // 5. Sort the inventors by years lived |
| 65 | + function compareYears(a, b) { |
| 66 | + return (a.passed - a.year) - (b.passed - b.year); |
| 67 | + } |
| 68 | + const yearsLived = inventors.sort(compareYears); |
| 69 | + console.log(yearsLived); |
45 | 70 |
|
46 | 71 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
47 | 72 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
48 | | - |
| 73 | + // const wrapper = document.querySelector(".mw-category"); |
| 74 | + // const links = Array.prototype.slice.call(wrapper.querySelectorAll("a")); |
| 75 | + // const boulevardNames = links.map(link => link.textContent); |
| 76 | + // const de = boulevardNames.filter(name => name.includes("de")); |
| 77 | + // console.log(de); |
49 | 78 |
|
50 | 79 | // 7. sort Exercise |
51 | 80 | // Sort the people alphabetically by last name |
| 81 | + function compareLast(a, b) { |
| 82 | + const lasta = a.split(", ")[0]; |
| 83 | + const lastb = b.split(", ")[0]; |
| 84 | + return lasta.localeCompare(lastb); |
| 85 | + } |
| 86 | + const sortedLast = people.sort(compareLast); |
| 87 | + console.log(sortedLast); |
52 | 88 |
|
53 | 89 | // 8. Reduce Exercise |
54 | 90 | // Sum up the instances of each of these |
55 | 91 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 92 | + function sumWords(obj, word) { |
| 93 | + if (obj[word] == null) { |
| 94 | + obj[word] = 1; |
| 95 | + } else { |
| 96 | + obj[word] += 1; |
| 97 | + } |
| 98 | + return obj; |
| 99 | + } |
| 100 | + const sumInstances = data.reduce(sumWords, {}); |
| 101 | + console.log(sumInstances); |
56 | 102 |
|
57 | 103 | </script> |
58 | 104 | </body> |
|
0 commit comments