diff --git a/04 - Array Cardio Day 1/index-START.html b/04 - Array Cardio Day 1/index-START.html index 0dcfd00f40..dca39d87b7 100644 --- a/04 - Array Cardio Day 1/index-START.html +++ b/04 - Array Cardio Day 1/index-START.html @@ -38,17 +38,31 @@ // Array.prototype.filter() // 1. Filter the list of inventors for those who were born in the 1500's + const fifthteens = inventors.filter((inv) => inv["year"] < 1600 && inv["year"] >= 1500); + // Array.prototype.map() // 2. Give us an array of the inventors first and last names + const firstLastNames = inventors.map((inv) => inv["first"] + ", " + inv["last"]); // Array.prototype.sort() // 3. Sort the inventors by birthdate, oldest to youngest + const sortedInventorsBirthday= inventors.sort((a, b) => a["year"] > b["year"] ? 1 : -1); + const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1); + console.table(ordered); // Array.prototype.reduce() // 4. How many years did all the inventors live all together? + const sumYearsLived = inventors.reduce((total, initialItem) => total + (initialItem["passed"] - initialItem["year"]), 0); // 5. Sort the inventors by years lived + const sortedInventorsByYearsLived = inventors.sort((last, next) => { + const lastInventorAge = (last["passed"] - last["year"]); + const nextInventorAge = (next["passed"] - next["year"]); + + return lastInventorAge > nextInventorAge ? 1 : -1; + }); + console.table(sortedInventorsByYearsLived); // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris @@ -56,11 +70,26 @@ // 7. sort Exercise // Sort the people alphabetically by last name + const peopleByAlphabetic = people.sort((last, next) => { + const [aLast, aFirst] = last.split(', '); + const [bLast, bFirst] = next.split(', '); + return aLast > bLast ? 1 : -1; + }); + console.log(peopleByAlphabetic); // 8. Reduce Exercise // Sum up the instances of each of these const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; - + // i got a wrong interpretation but i will leave the 'wrong' code behing because it ended up being a good exercise + //const sumInstancesOfData = data.reduce((reductor, currentElement) => reductor.includes(currentElement) ? reductor : [...reductor, currentElement], []) + //console.log(sumInstancesOfData.length); + const sumInstancesOfData = data.reduce(function(reductor, currentElement){ + if(!reductor[currentElement]){ + reductor[currentElement] = 0; + } + reductor[currentElement]++; + return reductor; + }, {}); diff --git a/07 - Array Cardio Day 2/index-START.html b/07 - Array Cardio Day 2/index-START.html index 4ca34c7536..1ecbbbff5c 100644 --- a/07 - Array Cardio Day 2/index-START.html +++ b/07 - Array Cardio Day 2/index-START.html @@ -27,15 +27,24 @@ // Some and Every Checks // Array.prototype.some() // is at least one person 19 or older? + const somePeople = people.some((obj) => 2024 - obj.year >= 19); + console.log(somePeople); + // Array.prototype.every() // is everyone 19 or older? + const everyPeople = people.every((obj) => 2024 - obj.year >= 19); + console.log(everyPeople); // Array.prototype.find() // Find is like filter, but instead returns just the one you are looking for // find the comment with the ID of 823423 + const specificComment = comments.find((obj) => obj.id === 823423); + console.log(specificComment); // Array.prototype.findIndex() // Find the comment with this ID // delete the comment with the ID of 823423 + const specificCommentIndex = comments.findIndex((obj) => obj.id === 823423); + comments.splice(specificCommentIndex, 1);