From b24ed50b43c9ab19d53dd02eadaed9c722de2781 Mon Sep 17 00:00:00 2001 From: NathanC90 Date: Wed, 10 Jan 2024 12:16:41 +0800 Subject: [PATCH 01/10] Add a function to filter who were born in the 1500s --- 04 - Array Cardio Day 1/index-START.html | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/04 - Array Cardio Day 1/index-START.html b/04 - Array Cardio Day 1/index-START.html index 0dcfd00f40..60a4a85eaa 100644 --- a/04 - Array Cardio Day 1/index-START.html +++ b/04 - Array Cardio Day 1/index-START.html @@ -38,7 +38,22 @@ // Array.prototype.filter() // 1. Filter the list of inventors for those who were born in the 1500's + function is1500() { + console.log(`The list of inventors for those who were born in the 1500's: `); + for(let i = 0; i < inventors.length; i++){ + let selected = inventors[i]; + // console.log(selected) + let yearOfBirth = selected.year; + if(yearOfBirth > 1499 && yearOfBirth < 1600){ + let the1500s = `${inventors[i].first} ${inventors[i].last}` + console.log(`${the1500s}`); + } + } + } + + const filtered = inventors.filter(is1500); + // Array.prototype.map() // 2. Give us an array of the inventors first and last names From 8545242b7bf8a1aec7c2919382d3bbfebb7f63e4 Mon Sep 17 00:00:00 2001 From: NathanC90 Date: Wed, 10 Jan 2024 12:27:26 +0800 Subject: [PATCH 02/10] Refactor the filter function --- 04 - Array Cardio Day 1/index-START.html | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/04 - Array Cardio Day 1/index-START.html b/04 - Array Cardio Day 1/index-START.html index 60a4a85eaa..2f30a1f871 100644 --- a/04 - Array Cardio Day 1/index-START.html +++ b/04 - Array Cardio Day 1/index-START.html @@ -38,17 +38,10 @@ // Array.prototype.filter() // 1. Filter the list of inventors for those who were born in the 1500's - function is1500() { - console.log(`The list of inventors for those who were born in the 1500's: `); - for(let i = 0; i < inventors.length; i++){ - - let selected = inventors[i]; - // console.log(selected) - let yearOfBirth = selected.year; - if(yearOfBirth > 1499 && yearOfBirth < 1600){ - let the1500s = `${inventors[i].first} ${inventors[i].last}` - console.log(`${the1500s}`); - } + function is1500(inventor) { + let yearOfBirth = inventor.year; + if (yearOfBirth > 1499 && yearOfBirth < 1600) { + console.table(inventor); } } From 39b950b9ef6b0eadfbca3fa4994ec25ebfebe41d Mon Sep 17 00:00:00 2001 From: NathanC90 Date: Wed, 10 Jan 2024 12:34:51 +0800 Subject: [PATCH 03/10] Use map() to show an array of the inventors first and last names --- 04 - Array Cardio Day 1/index-START.html | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/04 - Array Cardio Day 1/index-START.html b/04 - Array Cardio Day 1/index-START.html index 2f30a1f871..8c5877b783 100644 --- a/04 - Array Cardio Day 1/index-START.html +++ b/04 - Array Cardio Day 1/index-START.html @@ -50,6 +50,10 @@ // 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 From 5feaf12d6600a22470db5ca556bd3d79087546d3 Mon Sep 17 00:00:00 2001 From: NathanC90 Date: Wed, 10 Jan 2024 13:12:12 +0800 Subject: [PATCH 04/10] Sort the inventors by birthdate, oldest to youngest --- 04 - Array Cardio Day 1/index-START.html | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/04 - Array Cardio Day 1/index-START.html b/04 - Array Cardio Day 1/index-START.html index 8c5877b783..1cb2de4c34 100644 --- a/04 - Array Cardio Day 1/index-START.html +++ b/04 - Array Cardio Day 1/index-START.html @@ -57,6 +57,12 @@ // 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? From 9a2f65a28595cfad394a5e23b22dc6953bba202b Mon Sep 17 00:00:00 2001 From: NathanC90 Date: Wed, 10 Jan 2024 16:53:16 +0800 Subject: [PATCH 05/10] Use reduce() to get the total age of the inventors combined --- 04 - Array Cardio Day 1/index-START.html | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/04 - Array Cardio Day 1/index-START.html b/04 - Array Cardio Day 1/index-START.html index 1cb2de4c34..de2094ce92 100644 --- a/04 - Array Cardio Day 1/index-START.html +++ b/04 - Array Cardio Day 1/index-START.html @@ -66,6 +66,21 @@ // 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 // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name From 51c5b3ecf9b733f568b079f3a47ec75cf31cc472 Mon Sep 17 00:00:00 2001 From: NathanC90 Date: Wed, 10 Jan 2024 17:45:12 +0800 Subject: [PATCH 06/10] Sort the inventors by years lived --- 04 - Array Cardio Day 1/index-START.html | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/04 - Array Cardio Day 1/index-START.html b/04 - Array Cardio Day 1/index-START.html index de2094ce92..4d77309756 100644 --- a/04 - Array Cardio Day 1/index-START.html +++ b/04 - Array Cardio Day 1/index-START.html @@ -83,6 +83,21 @@ // 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 From ad89398c8eeab7aa05253b0e0e7373250b995c8c Mon Sep 17 00:00:00 2001 From: NathanC90 Date: Wed, 10 Jan 2024 19:02:07 +0800 Subject: [PATCH 07/10] Complete exercise 6 --- 04 - Array Cardio Day 1/index-START.html | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/04 - Array Cardio Day 1/index-START.html b/04 - Array Cardio Day 1/index-START.html index 4d77309756..1f9aadc447 100644 --- a/04 - Array Cardio Day 1/index-START.html +++ b/04 - Array Cardio Day 1/index-START.html @@ -100,6 +100,13 @@ // 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 From cfe664e1e362f690758f80640b0ad55de238510a Mon Sep 17 00:00:00 2001 From: NathanC90 Date: Wed, 10 Jan 2024 19:11:29 +0800 Subject: [PATCH 08/10] Comment out #6 codes and sort #7 names alphabetically --- 04 - Array Cardio Day 1/index-START.html | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/04 - Array Cardio Day 1/index-START.html b/04 - Array Cardio Day 1/index-START.html index 1f9aadc447..8caee35565 100644 --- a/04 - Array Cardio Day 1/index-START.html +++ b/04 - Array Cardio Day 1/index-START.html @@ -100,18 +100,21 @@ // 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; - }); + // 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')); + // 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' ]; From 8471b074fef8448b85d0fc2966d21f0c5a80d217 Mon Sep 17 00:00:00 2001 From: NathanC90 Date: Wed, 10 Jan 2024 21:19:32 +0800 Subject: [PATCH 09/10] Use reduce() to sum up the instances of each car --- 04 - Array Cardio Day 1/index-START.html | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/04 - Array Cardio Day 1/index-START.html b/04 - Array Cardio Day 1/index-START.html index 8caee35565..f91078f30a 100644 --- a/04 - Array Cardio Day 1/index-START.html +++ b/04 - Array Cardio Day 1/index-START.html @@ -119,6 +119,17 @@ // 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); + From d18923caf9e2228e27312fb212825a82653a1cf7 Mon Sep 17 00:00:00 2001 From: NathanC90 Date: Wed, 10 Jan 2024 22:14:55 +0800 Subject: [PATCH 10/10] Complete all exercises --- 07 - Array Cardio Day 2/index-START.html | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) 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); +