diff --git a/04 - Array Cardio Day 1/index-START.html b/04 - Array Cardio Day 1/index-START.html index 0dcfd00f40..f37b5e4877 100644 --- a/04 - Array Cardio Day 1/index-START.html +++ b/04 - Array Cardio Day 1/index-START.html @@ -38,29 +38,50 @@ // Array.prototype.filter() // 1. Filter the list of inventors for those who were born in the 1500's + console.table(inventors.filter((it) => it.year >= 1500 && it.year < 1600)) // Array.prototype.map() // 2. Give us an array of the inventors first and last names - + console.table(inventors.map((it) => `${it.first} ${it.last}`)) + // Array.prototype.sort() - // 3. Sort the inventors by birthdate, oldest to youngest + // 3. Sort the inventors by birthdate, oldest to youngest [descending] + console.table(inventors.sort((b, a) => a.year > b.year ? 1 : -1)) // Array.prototype.reduce() // 4. How many years did all the inventors live all together? + console.table(inventors.reduce((acc, it) => acc += (it.passed - it.year), 0)) - // 5. Sort the inventors by years lived - + // 5. Sort the inventors by years lived [descending] + console.table(inventors.sort((b, a) => (a.passed - a.year) > (b.passed - b.year) ? 1 : -1)) + // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name - // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris - + // from source: https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris + // get class ".mw-category" DOM element + let category = document.querySelector(".mw-category") + // get node with all "a" DOM elements + let linksNode = category.querySelectorAll("a") + // convert node to list & map each element to its text content + let blvdNames = Array.from(linksNode).map(it => it.textContent) + // filter list of boulevard names with those containing "de" in name + let deNames = blvdNames.filter((it) => it.includes("de")) + console.table(deNames) // 7. sort Exercise - // Sort the people alphabetically by last name + // Sort the people alphabetically by last name [ascending] + console.table(people.sort((a,b) => a > b ? 1 : -1)) // 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' ]; - + // create empty object & properties for each string & cumulative count + console.table( + data.reduce((obj, it) => { + if (!obj[it]) { obj[it] = 0 } + obj[it]++ + return obj + }, {}) + ) diff --git a/07 - Array Cardio Day 2/index-START.html b/07 - Array Cardio Day 2/index-START.html index 4ca34c7536..67433ff807 100644 --- a/07 - Array Cardio Day 2/index-START.html +++ b/07 - Array Cardio Day 2/index-START.html @@ -25,18 +25,25 @@ { text: 'Nice Nice Nice!', id: 542328 } ]; + // return person age + let getAge = (personObj) => (new Date()).getFullYear() - personObj.year // Some and Every Checks // Array.prototype.some() // is at least one person 19 or older? + console.log(people.some((it) => getAge(it) >= 19)) // true // Array.prototype.every() // is everyone 19 or older? + console.log(people.every((it) => getAge(it) >= 19)) // false // 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 - + console.table(comments.find((it) => it.id == 823423)) + // Array.prototype.findIndex() // Find the comment with this ID // delete the comment with the ID of 823423 - + let index = comments.findIndex((it) => it.id == 823423) + comments.splice(index, 1) + console.table(comments)