diff --git a/04 - Array Cardio Day 1/index-START.html b/04 - Array Cardio Day 1/index-START.html index 0dcfd00f40..f91078f30a 100644 --- a/04 - Array Cardio Day 1/index-START.html +++ b/04 - Array Cardio Day 1/index-START.html @@ -38,29 +38,98 @@ // Array.prototype.filter() // 1. Filter the list of inventors for those who were born in the 1500's + function is1500(inventor) { + let yearOfBirth = inventor.year; + if (yearOfBirth > 1499 && yearOfBirth < 1600) { + console.table(inventor); + } + } + const filtered = inventors.filter(is1500); + // Array.prototype.map() // 2. Give us an array of the inventors first and last names + const firstName = inventors.map(function firstAndLast(inventor){ + console.log(`${inventor.first} ${inventor.last}`); + }) + // Array.prototype.sort() // 3. Sort the inventors by birthdate, oldest to youngest + const oldToYoung = inventors.sort(function(inventorOne,inventorTwo){ + return inventorOne.year - inventorTwo.year; + }); + + console.table(oldToYoung); + // Array.prototype.reduce() // 4. How many years did all the inventors live all together? + const ageOfInventors = [] + + for(let i = 0; i < inventors.length; i++) { + let age = inventors[i].passed - inventors[i].year; + ageOfInventors.push(age); + } + + const sum = ageOfInventors.reduce(sumAll); + + function sumAll(total, value) { + return total+value; + } + + console.log(`The number of the years that all the inventors live together is: ${sum}`); + // 5. Sort the inventors by years lived + function sortByYearsLived(inventorOne, inventorTwo){ + let firstPerson = inventorOne.passed - inventorOne.year; + let secondPerson = inventorTwo.passed - inventorTwo.year; + + if(firstPerson > secondPerson) { + return -1; + } else { + return 1; // bubble up + } + } + + const age = inventors.sort(sortByYearsLived); + + console.table(age); + // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris + // const div = document.querySelector('.mw-category'); + // const links = Array.from(div.querySelectorAll('a')); + // let de = links.map(function(link){ + // return link.textContent; + // }); + + // de.filter(hasDe => hasDe.includes('de')); // 7. sort Exercise // Sort the people alphabetically by last name + const sortedNames = people.sort(); + console.log(sortedNames); + // 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' ]; + let instances = data.reduce(function(allCars, car) { + if(car in allCars) { + allCars[car]++; + } else { + allCars[car] = 1; + } + return allCars; + }, {}) + + console.log(instances); + diff --git a/07 - Array Cardio Day 2/index-START.html b/07 - Array Cardio Day 2/index-START.html index 4ca34c7536..0c462e271e 100644 --- a/07 - Array Cardio Day 2/index-START.html +++ b/07 - Array Cardio Day 2/index-START.html @@ -27,16 +27,50 @@ // Some and Every Checks // Array.prototype.some() // is at least one person 19 or older? + const currentYear = (new Date()).getFullYear(); + const nineteen = people.some(function(person) { + let age = currentYear - person.year; + if(age >= 19) { + return true; + } + }) + + console.log(nineteen); + // Array.prototype.every() // is everyone 19 or older? + const allNineteen = people.every(function(person) { + let age = currentYear - person.year; + if(age >= 19) { + return true; + } + }) + + console.log(allNineteen); // 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 targetId = comments.find(function(comment) { + if(comment.id === 823423) { + return comment; + } + }) + + console.log(targetId); // Array.prototype.findIndex() // Find the comment with this ID // delete the comment with the ID of 823423 + const targetComment = comments.findIndex(function(comment) { + if(comment.id === 823423) { + return true; + } + }) + console.log(targetComment); + + comments.splice(targetComment, 1); +