|
37 | 37 |
|
38 | 38 | // Array.prototype.filter() |
39 | 39 | // 1. Filter the list of inventors for those who were born in the 1500's |
| 40 | + const inventorsBornInFifteenHundreds = inventors.filter((inventor) => inventor.year > 1500 && inventor.year < 1600) |
| 41 | + console.log("inventorsBornInFifteenHundreds",inventorsBornInFifteenHundreds) |
40 | 42 |
|
41 | 43 | // Array.prototype.map() |
42 | 44 | // 2. Give us an array of the inventors first and last names |
| 45 | + const inventorsFirstLastNames = inventors.map((inventor) => `${inventor.first} ${inventor.last}`) |
| 46 | + console.log("inventorsFirstLastNames",inventorsFirstLastNames) |
43 | 47 |
|
44 | 48 | // Array.prototype.sort() |
45 | 49 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 50 | + const inventorsBirthDateOldestToYoungest = inventors.sort((a, b) => a.year > b.year ? 1 : -1) |
| 51 | + console.log("inventorsBirthDateOldestToYoungest",inventorsBirthDateOldestToYoungest) |
46 | 52 |
|
47 | 53 | // Array.prototype.reduce() |
48 | 54 | // 4. How many years did all the inventors live all together? |
| 55 | + const inventorsAgeCombined = inventors.reduce((prev, inventor) => ((inventor.passed - inventor.year) + prev), 0) |
| 56 | + console.log(inventorsAgeCombined) |
49 | 57 |
|
50 | 58 | // 5. Sort the inventors by years lived |
| 59 | + const inventorsSortedByYearsLived = inventors.sort((a, b) => ((a.passed - a.year) < (b.passed - b.year) ? 1 : -1)) |
| 60 | + console.log("inventorsSortedByYearsLived",inventorsSortedByYearsLived) |
| 61 | + console.log("inventorsSortedByYearsLived - with age",inventorsSortedByYearsLived.map((i) => {i["age"] = i.passed - i.year; return i})) |
51 | 62 |
|
52 | 63 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
53 | 64 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
54 | | - |
| 65 | + // const boulevards = [ |
| 66 | + // "Boulevards of Paris", |
| 67 | + // "City walls of Paris", |
| 68 | + // "Thiers wall", |
| 69 | + // "Wall of Charles V", |
| 70 | + // "Wall of Philip II Augustus", |
| 71 | + // "City gates of Paris", |
| 72 | + // "Haussmann's renovation of Paris", |
| 73 | + // "Boulevards of the Marshals", |
| 74 | + // "Boulevard Auguste-Blanqui", |
| 75 | + // "Boulevard Barbès", |
| 76 | + // "Boulevard Beaumarchais", |
| 77 | + // "Boulevard de l'Amiral-Bruix", |
| 78 | + // "Boulevard Mortier", |
| 79 | + // "Boulevard Poniatowski", |
| 80 | + // "Boulevard Soult", |
| 81 | + // "Boulevard des Capucines", |
| 82 | + // "Boulevard de la Chapelle", |
| 83 | + // "Boulevard de Clichy", |
| 84 | + // "Boulevard du Crime", |
| 85 | + // "Boulevard du Général-d'Armée-Jean-Simon", |
| 86 | + // "Boulevard Haussmann", |
| 87 | + // "Boulevard de l'Hôpital", |
| 88 | + // "Boulevard des Italiens", |
| 89 | + // "Boulevard Lefebvre", |
| 90 | + // "Boulevard de la Madeleine", |
| 91 | + // "Boulevard de Magenta", |
| 92 | + // "Boulevard Malesherbes", |
| 93 | + // "Boulevard Marguerite-de-Rochechouart", |
| 94 | + // "Boulevard Montmartre", |
| 95 | + // "Boulevard du Montparnasse", |
| 96 | + // "Boulevard Raspail", |
| 97 | + // "Boulevard Richard-Lenoir", |
| 98 | + // "Boulevard Saint-Germain", |
| 99 | + // "Boulevard Saint-Michel", |
| 100 | + // "Boulevard de Sébastopol", |
| 101 | + // "Boulevard de Strasbourg", |
| 102 | + // "Boulevard du Temple", |
| 103 | + // "Boulevard Voltaire", |
| 104 | + // "Boulevard de la Zone" |
| 105 | + // ] |
| 106 | + // const deBoulevards = boulevards.filter((boulevard) => /de/i.test(boulevard)) |
| 107 | + // console.log("deBoulevards",deBoulevards) |
55 | 108 |
|
56 | 109 | // 7. sort Exercise |
57 | 110 | // Sort the people alphabetically by last name |
| 111 | + const sortedPeople = people.sort((a, b) => a.split(",")[0] > b.split(",")[0] ? 1 : -1) |
| 112 | + console.log("sortedPeople",sortedPeople) |
| 113 | + |
58 | 114 |
|
59 | 115 | // 8. Reduce Exercise |
60 | 116 | // Sum up the instances of each of these |
61 | 117 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 118 | + // const reducedExercise = data.reduce((vehicleCounts, vehicle) => (!vehicleCounts[vehicle] ? vehicleCounts[vehicle] = 1 : vehicleCounts[vehicle] += 1), {}) |
| 119 | + const reducedExercise = data.reduce((vehicleCounts, vehicle) => { |
| 120 | + if(!vehicleCounts[vehicle]){ |
| 121 | + vehicleCounts[vehicle] = 1 |
| 122 | + }else{ |
| 123 | + vehicleCounts[vehicle] += 1 |
| 124 | + } |
| 125 | + return vehicleCounts}, {}) |
| 126 | + console.log("reducedExercise", reducedExercise) |
62 | 127 |
|
63 | 128 | </script> |
64 | 129 | </body> |
|
0 commit comments