From e682e5ae695dd0a6b12351a449e5fb06d0737562 Mon Sep 17 00:00:00 2001 From: Orlando Castillo Date: Tue, 14 Feb 2023 05:04:00 -0500 Subject: [PATCH 1/8] Task 1 Completed --- index.js | 239 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 125 insertions(+), 114 deletions(-) diff --git a/index.js b/index.js index 00027a1c8..e287f0d14 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ //🚀🚀🚀 Topic #1 Closures 🚀🚀🚀// /* 🚀🚀🚀🤓 Task 1: 🤓🚀🚀🚀 Study the code below and explain in your own words why nested function can access the variable internal. */ +// Because nestedFunction is being invoked inside myFunction and due to lexical scoping, any function inside another function has access to its variables. const external = "I'm outside the function"; @@ -15,11 +16,7 @@ function myFunction() { } //myFunction(); -//🚀🚀🚀 ⬇️ 📝 Explanation ⬇️ 📝 🚀🚀🚀: - - - - +//🚀🚀🚀 ⬇️ 📝 Explanation ⬇️ 📝 🚀🚀🚀: /* 🚀🚀🚀 Task 2: Counter 🚀🚀🚀 */ /* Use summation to do the following: @@ -32,27 +29,75 @@ function myFunction() { function summation(/*Your Code Here*/) { /*Your Code Here*/ - - } - +} // 🦁🦁🦁 Topic 2: ADVANCED Array Methods 🦁🦁🦁 // Given this zoo data from around the United States, follow the instructions below. Use the specific array methods in the requests below to solve the problems. const zooAnimals = [ - { animal_name: "Jackal, asiatic", population: 5, scientific_name: "Canis aureus", state: "Kentucky" }, - { animal_name: "Screamer, southern", population: 1, scientific_name: "Chauna torquata", state: "Alabama" }, - { animal_name: "White spoonbill", population: 8, scientific_name: "Platalea leucordia", state: "Georgia" }, - { animal_name: "White-cheeked pintail", population: 1, scientific_name: "Anas bahamensis", state: "Oregon" }, - { animal_name: "Black-backed jackal", population: 2, scientific_name: "Canis mesomelas", state: "Washington" }, - { animal_name: "Brolga crane", population: 9, scientific_name: "Grus rubicundus", state: "New Mexico" }, - { animal_name: "Common melba finch", population: 5, scientific_name: "Pytilia melba", state: "Pennsylvania" }, - { animal_name: "Pampa gray fox", population: 10, scientific_name: "Pseudalopex gymnocercus", state: "Connecticut" }, - { animal_name: "Hawk-eagle, crowned", population: 10, scientific_name: "Spizaetus coronatus", state: "Florida" }, - { animal_name: "Australian pelican", population: 5, scientific_name: "Pelecanus conspicillatus", state: "West Virginia" }, - ]; - - /* 🦁🦁🦁 Request 1: .forEach() 🦁🦁🦁 + { + animal_name: 'Jackal, asiatic', + population: 5, + scientific_name: 'Canis aureus', + state: 'Kentucky', + }, + { + animal_name: 'Screamer, southern', + population: 1, + scientific_name: 'Chauna torquata', + state: 'Alabama', + }, + { + animal_name: 'White spoonbill', + population: 8, + scientific_name: 'Platalea leucordia', + state: 'Georgia', + }, + { + animal_name: 'White-cheeked pintail', + population: 1, + scientific_name: 'Anas bahamensis', + state: 'Oregon', + }, + { + animal_name: 'Black-backed jackal', + population: 2, + scientific_name: 'Canis mesomelas', + state: 'Washington', + }, + { + animal_name: 'Brolga crane', + population: 9, + scientific_name: 'Grus rubicundus', + state: 'New Mexico', + }, + { + animal_name: 'Common melba finch', + population: 5, + scientific_name: 'Pytilia melba', + state: 'Pennsylvania', + }, + { + animal_name: 'Pampa gray fox', + population: 10, + scientific_name: 'Pseudalopex gymnocercus', + state: 'Connecticut', + }, + { + animal_name: 'Hawk-eagle, crowned', + population: 10, + scientific_name: 'Spizaetus coronatus', + state: 'Florida', + }, + { + animal_name: 'Australian pelican', + population: 5, + scientific_name: 'Pelecanus conspicillatus', + state: 'West Virginia', + }, +]; + +/* 🦁🦁🦁 Request 1: .forEach() 🦁🦁🦁 The zoos want to display both the scientific name and the animal name in front of the habitats. 1. Receive the zooAnimals array as an argument passed from a parameter 2. Use .forEach() to populate a new array called displayNames that will be an array of strings with only the animal name and scientific name of each animal @@ -60,12 +105,11 @@ const zooAnimals = [ 💡 NOTE: the array returned should be an array of strings, and each string should follow this pattern: "name: {name}, scientific: {scientific name}" */ - function animalNames(/*Your Code Here*/){ - /*Your Code Here*/ - } - +function animalNames(/*Your Code Here*/) { + /*Your Code Here*/ +} - /* 🦁🦁🦁 Request 2: .map() 🦁🦁🦁 +/* 🦁🦁🦁 Request 2: .map() 🦁🦁🦁 The zoo needs a list of all their animal's names converted to lower case. Use lowerCaseNames to do the following: 1. Receive the zooAnimals array as an argument passed from a parameter @@ -75,12 +119,11 @@ const zooAnimals = [ 💡 NOTE: Do some research for other methods that can help help you */ - function lowerCaseNames(/*Your Code Here*/){ - /*Your Code Here*/ - } - - - /* 🦁🦁🦁 Request 3: .filter() 🦁🦁🦁 +function lowerCaseNames(/*Your Code Here*/) { + /*Your Code Here*/ +} + +/* 🦁🦁🦁 Request 3: .filter() 🦁🦁🦁 The zoo is concerned about animals with a lower population count. Use lowPopulationAnimals to do the following: 1. Receive the zooAnimals array as an argument passed from a parameter @@ -88,12 +131,11 @@ const zooAnimals = [ 3. Return this new array */ - function lowPopulationAnimals(/*Your Code Here*/){ - /*Your Code Here*/ - } - +function lowPopulationAnimals(/*Your Code Here*/) { + /*Your Code Here*/ +} - /* 🦁🦁🦁 Request 4: .reduce() 🦁🦁🦁 +/* 🦁🦁🦁 Request 4: .reduce() 🦁🦁🦁 The zoo needs to know their total animal population across the United States. USe USApop to do the following: 1. Receive the zooAnimals array as an argument passed from a parameter @@ -102,13 +144,12 @@ const zooAnimals = [ 💡 NOTE: Remember the reduce method takes two arguments: a callback (which itself takes two args - the accumulator and the item), and an initial value for the count. Check MDN/W3Schools for syntax! */ - function USApop(/*Your Code Here*/){ - /*Your Code Here*/ - } - - - // 🦁🦁🦁 Callbacks 🦁🦁🦁 - /* 🦁🦁🦁 Step 1: Create a higher-order function 🦁🦁🦁 +function USApop(/*Your Code Here*/) { + /*Your Code Here*/ +} + +// 🦁🦁🦁 Callbacks 🦁🦁🦁 +/* 🦁🦁🦁 Step 1: Create a higher-order function 🦁🦁🦁 Use the higher-order function called consume to do the following: 1. Receive 3 parameters: a, b and cb. The first two parameters (a and b) can take any argument (we can pass any value as an argument) and the last parameter (cb) accepts a callback 2. Return the invocation of cb taking `a` and `b` as its arguments @@ -116,52 +157,46 @@ const zooAnimals = [ 💡 NOTE: The tests for 'consume' will pass if it is created correctly and also after you correctly complete the functions 'add' and 'greeting' below in Step 2. */ - function consume(/*Your Code Here */){ - /*Your Code Here */ - } - - - // 🦁🦁🦁 Step 2: Create several functions to callback with consume(); 🦁🦁🦁 +function consume(/*Your Code Here */) { + /*Your Code Here */ +} - /* Use add to do the following: +// 🦁🦁🦁 Step 2: Create several functions to callback with consume(); 🦁🦁🦁 + +/* Use add to do the following: 1. Receive two numbers as an argument that are passed in from its first and second parameters 2. Return the sum of those numbers */ -function add(/*Your Code Here */){ - /*Your Code Here*/ - } - +function add(/*Your Code Here */) { + /*Your Code Here*/ +} /* Use multiply to do the following: 1. Receive two numbers as an argument that are passed in from its first and second parameters 2. Return the product of those numbers */ -function multiply(/*Your Code Here */){ - /*Your Code Here */ - } - +function multiply(/*Your Code Here */) { + /*Your Code Here */ +} - /* Use greeting to do the following: +/* Use greeting to do the following: 1. Receive two strings (a first name and last name) as an argument that are passed in from its first and second parameters 2. Return "Hello {first-name} {last-name}, nice to meet you!" 💡 NOTE: The string returned must match the format above or the test will not pass! */ -function greeting(/*Your Code Here */){ - return /*Your Code Here */ - } - - -// 🦁🦁🦁 Step 3: Check your work by un-commenting the following calls to consume(): 🦁🦁🦁 +function greeting(/*Your Code Here */) { + return; /*Your Code Here */ +} + +// 🦁🦁🦁 Step 3: Check your work by un-commenting the following calls to consume(): 🦁🦁🦁 // ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ // console.log(consume(2, 2, add)); // 4 // console.log(consume(10, 16, multiply)); // 160 // console.log(consume("Mary", "Poppins", greeting)); // Hello Mary Poppins, nice to meet you! - - // 🐴🐴🐴 Topic 3: Prototypes 🐴🐴🐴 // //🐴🐴🐴 Task: You are to build a cuboid maker that can return values for a cuboid's volume or surface area. Cuboids are similar to cubes but do not have even sides. Follow the steps in order to accomplish this challenge. 🐴🐴🐴 @@ -175,79 +210,55 @@ function greeting(/*Your Code Here */){ - Instances of CuboidMaker should initialize `length`, `width` and `height` properties */ -function CuboidMaker(/*Your Code Here */){ +function CuboidMaker(/*Your Code Here */) { /*Your Code Here */ } - /* 🐴🐴🐴 Step 2: Volume Method 🐴🐴🐴 Create a method called volume using CuboidMaker's prototype that returns the volume of a given cuboid's length, width, and height 💡 NOTE: Formula for cuboid volume: length * width * height */ - - - /* 🐴🐴🐴 Step 3: Surface Area Method 🐴🐴🐴 Create another method called surfaceArea using CuboidMaker's prototype that returns the surface area of a given cuboid's length, width, and height. 💡 NOTE: Formula for cuboid surface area: 2 * (length * width + length * height + width * height) */ - - - /* 🐴🐴🐴 Step 4: Create a new object that uses CuboidMaker (not auto graded)🐴🐴🐴 Create an object called cuboid that uses the new keyword to use our CuboidMaker constructor Add properties and values of length: 4, width: 5, and height: 5 to cuboid. */ - - - - // 🐴🐴🐴 Test your volume and surfaceArea methods by uncommenting the logs below: 🐴🐴🐴 // ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ // console.log(cuboid.volume()); // 100 // console.log(cuboid.surfaceArea()); // 130 - // 🦄🦄🦄 Topic 4: Classes 🦄🦄🦄 // //Using CuboidMakerTwo, take your prototypes from above and refactor into class syntax. Then, create an object called cuboidTwo that uses the new keyword to use our CuboidMakerTwo class. - -class CuboidMakerTwo{ - -} - - +class CuboidMakerTwo {} //🦄🦄🦄 Test your volume and surfaceArea methods by uncommenting the logs below: 🦄🦄🦄 // console.log(cuboidTwo.volume()); // 100 // console.log(cuboidTwo.surfaceArea()); // 130 - - - - - - - - /* 🛑🛑🛑🛑🛑 Please do not modify anything below this line 🛑🛑🛑🛑🛑 */ - function foo(){ - console.log('its working'); - return 'bar'; - } - foo(); - module.exports = { - foo, - summation, - animalNames, - lowerCaseNames, - lowPopulationAnimals, - USApop, - consume, - add, - multiply, - greeting, - CuboidMaker, - CuboidMakerTwo - } +/* 🛑🛑🛑🛑🛑 Please do not modify anything below this line 🛑🛑🛑🛑🛑 */ +function foo() { + console.log('its working'); + return 'bar'; +} +foo(); +module.exports = { + foo, + summation, + animalNames, + lowerCaseNames, + lowPopulationAnimals, + USApop, + consume, + add, + multiply, + greeting, + CuboidMaker, + CuboidMakerTwo, +}; From ee9ee170df2bd73df5a8986c8056bcc6b1ce8fd0 Mon Sep 17 00:00:00 2001 From: Orlando Castillo Date: Tue, 14 Feb 2023 05:20:07 -0500 Subject: [PATCH 2/8] Task 2 Completed --- index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index e287f0d14..65d1ba376 100644 --- a/index.js +++ b/index.js @@ -27,8 +27,13 @@ function myFunction() { 💡 NOTE: you may use a for loop for this function if you wish */ -function summation(/*Your Code Here*/) { +function summation(num) { /*Your Code Here*/ + if (num === 1) { + return 1; + } else { + return num + summation(num - 1); + } } // 🦁🦁🦁 Topic 2: ADVANCED Array Methods 🦁🦁🦁 From fbd7c3232d232f2e51b4e4af4ec6deb28c56c7c3 Mon Sep 17 00:00:00 2001 From: Orlando Castillo Date: Tue, 14 Feb 2023 05:37:19 -0500 Subject: [PATCH 3/8] Request 1 Completed --- index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 65d1ba376..af92d9e91 100644 --- a/index.js +++ b/index.js @@ -110,10 +110,19 @@ const zooAnimals = [ 💡 NOTE: the array returned should be an array of strings, and each string should follow this pattern: "name: {name}, scientific: {scientific name}" */ -function animalNames(/*Your Code Here*/) { +function animalNames(arr) { /*Your Code Here*/ + const displayNames = []; + arr.forEach((animal) => { + return displayNames.push( + `name: ${animal.animal_name}, scientific: ${animal.scientific_name}` + ); + }); + return displayNames; } +console.log(animalNames(zooAnimals)); + /* 🦁🦁🦁 Request 2: .map() 🦁🦁🦁 The zoo needs a list of all their animal's names converted to lower case. Use lowerCaseNames to do the following: From 1f122cdffd0f139e42fb5f51a2d14cff331c6629 Mon Sep 17 00:00:00 2001 From: Orlando Castillo Date: Tue, 14 Feb 2023 05:42:55 -0500 Subject: [PATCH 4/8] Request 2 Completed --- index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index af92d9e91..7fd27174c 100644 --- a/index.js +++ b/index.js @@ -133,8 +133,12 @@ console.log(animalNames(zooAnimals)); 💡 NOTE: Do some research for other methods that can help help you */ -function lowerCaseNames(/*Your Code Here*/) { +function lowerCaseNames(arr) { /*Your Code Here*/ + const lowerCaseAnimals = arr.map((animal) => { + return animal.animal_name.toLowerCase(); + }); + return lowerCaseAnimals; } /* 🦁🦁🦁 Request 3: .filter() 🦁🦁🦁 From d0414c5ada207cf27d070ae8756be1b28b98b22e Mon Sep 17 00:00:00 2001 From: Orlando Castillo Date: Tue, 14 Feb 2023 05:46:58 -0500 Subject: [PATCH 5/8] Request 3 Completed --- index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 7fd27174c..70a458dbd 100644 --- a/index.js +++ b/index.js @@ -149,8 +149,12 @@ function lowerCaseNames(arr) { 3. Return this new array */ -function lowPopulationAnimals(/*Your Code Here*/) { +function lowPopulationAnimals(arr) { /*Your Code Here*/ + const lowCount = arr.filter((animal) => { + return animal.population < 5; + }); + return lowCount; } /* 🦁🦁🦁 Request 4: .reduce() 🦁🦁🦁 From 4d78d60fc5f11bb4841683d8f9e2a4a0d4c9b9ca Mon Sep 17 00:00:00 2001 From: Orlando Castillo Date: Tue, 14 Feb 2023 05:54:11 -0500 Subject: [PATCH 6/8] Request 4 Completed --- index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 70a458dbd..e9b7fa871 100644 --- a/index.js +++ b/index.js @@ -166,8 +166,12 @@ function lowPopulationAnimals(arr) { 💡 NOTE: Remember the reduce method takes two arguments: a callback (which itself takes two args - the accumulator and the item), and an initial value for the count. Check MDN/W3Schools for syntax! */ -function USApop(/*Your Code Here*/) { +function USApop(arr) { /*Your Code Here*/ + const total = arr.reduce((acc, currVal) => { + return (acc += currVal.population); + }, 0); + return total; } // 🦁🦁🦁 Callbacks 🦁🦁🦁 From 46fdff190e08cb380863e97f219edbfe0a62a560 Mon Sep 17 00:00:00 2001 From: Orlando Castillo Date: Tue, 14 Feb 2023 06:02:07 -0500 Subject: [PATCH 7/8] Steps 1 and 2 completed --- index.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index e9b7fa871..47a714d87 100644 --- a/index.js +++ b/index.js @@ -183,8 +183,9 @@ function USApop(arr) { 💡 NOTE: The tests for 'consume' will pass if it is created correctly and also after you correctly complete the functions 'add' and 'greeting' below in Step 2. */ -function consume(/*Your Code Here */) { +function consume(a, b, cb) { /*Your Code Here */ + return cb(a, b); } // 🦁🦁🦁 Step 2: Create several functions to callback with consume(); 🦁🦁🦁 @@ -194,8 +195,9 @@ function consume(/*Your Code Here */) { 2. Return the sum of those numbers */ -function add(/*Your Code Here */) { +function add(n1, n2) { /*Your Code Here*/ + return n1 + n2; } /* Use multiply to do the following: @@ -203,8 +205,9 @@ function add(/*Your Code Here */) { 2. Return the product of those numbers */ -function multiply(/*Your Code Here */) { +function multiply(n1, n2) { /*Your Code Here */ + return n1 * n2; } /* Use greeting to do the following: @@ -213,8 +216,8 @@ function multiply(/*Your Code Here */) { 💡 NOTE: The string returned must match the format above or the test will not pass! */ -function greeting(/*Your Code Here */) { - return; /*Your Code Here */ +function greeting(firstName, lastName) { + return `Hello ${firstName} ${lastName}, nice to meet you!`; } // 🦁🦁🦁 Step 3: Check your work by un-commenting the following calls to consume(): 🦁🦁🦁 From 189be6d24b8d6bcaf2bddf2ae32c87e6ad0a3282 Mon Sep 17 00:00:00 2001 From: Orlando Castillo Date: Tue, 14 Feb 2023 06:18:02 -0500 Subject: [PATCH 8/8] MVP --- index.js | 51 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 47a714d87..d7bbaed5c 100644 --- a/index.js +++ b/index.js @@ -222,9 +222,9 @@ function greeting(firstName, lastName) { // 🦁🦁🦁 Step 3: Check your work by un-commenting the following calls to consume(): 🦁🦁🦁 // ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ -// console.log(consume(2, 2, add)); // 4 -// console.log(consume(10, 16, multiply)); // 160 -// console.log(consume("Mary", "Poppins", greeting)); // Hello Mary Poppins, nice to meet you! +console.log(consume(2, 2, add)); // 4 +console.log(consume(10, 16, multiply)); // 160 +console.log(consume('Mary', 'Poppins', greeting)); // Hello Mary Poppins, nice to meet you! // 🐴🐴🐴 Topic 3: Prototypes 🐴🐴🐴 // @@ -239,8 +239,11 @@ function greeting(firstName, lastName) { - Instances of CuboidMaker should initialize `length`, `width` and `height` properties */ -function CuboidMaker(/*Your Code Here */) { +function CuboidMaker(attr) { /*Your Code Here */ + this.length = attr.length; + this.width = attr.width; + this.height = attr.height; } /* 🐴🐴🐴 Step 2: Volume Method 🐴🐴🐴 @@ -248,24 +251,58 @@ function CuboidMaker(/*Your Code Here */) { 💡 NOTE: Formula for cuboid volume: length * width * height */ +CuboidMaker.prototype.volume = function () { + return this.length * this.width * this.height; +}; + /* 🐴🐴🐴 Step 3: Surface Area Method 🐴🐴🐴 Create another method called surfaceArea using CuboidMaker's prototype that returns the surface area of a given cuboid's length, width, and height. 💡 NOTE: Formula for cuboid surface area: 2 * (length * width + length * height + width * height) */ +CuboidMaker.prototype.surfaceArea = function () { + return ( + 2 * + (this.length * this.width + + this.length * this.height + + this.width * this.height) + ); +}; + /* 🐴🐴🐴 Step 4: Create a new object that uses CuboidMaker (not auto graded)🐴🐴🐴 Create an object called cuboid that uses the new keyword to use our CuboidMaker constructor Add properties and values of length: 4, width: 5, and height: 5 to cuboid. */ +const cuboid = new CuboidMaker(4, 5, 5); + // 🐴🐴🐴 Test your volume and surfaceArea methods by uncommenting the logs below: 🐴🐴🐴 // ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ -// console.log(cuboid.volume()); // 100 -// console.log(cuboid.surfaceArea()); // 130 +console.log(cuboid.volume()); // 100 +console.log(cuboid.surfaceArea()); // 130 // 🦄🦄🦄 Topic 4: Classes 🦄🦄🦄 // //Using CuboidMakerTwo, take your prototypes from above and refactor into class syntax. Then, create an object called cuboidTwo that uses the new keyword to use our CuboidMakerTwo class. -class CuboidMakerTwo {} +class CuboidMakerTwo { + constructor(attr) { + this.length = attr.length; + this.width = attr.width; + this.height = attr.height; + } + volume() { + return this.length * this.width * this.height; + } + surfaceArea() { + return ( + 2 * + (this.length * this.width + + this.length * this.height + + this.width * this.height) + ); + } +} + +const cuboidTwo = new CuboidMakerTwo(4, 5, 5); //🦄🦄🦄 Test your volume and surfaceArea methods by uncommenting the logs below: 🦄🦄🦄 // console.log(cuboidTwo.volume()); // 100