From 9edb5f716666bc390e584e1d942d2da5c335503e Mon Sep 17 00:00:00 2001 From: extrude575757 Date: Fri, 14 Aug 2020 18:10:28 -0700 Subject: [PATCH 01/10] Start off --- sprint2Qs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 sprint2Qs diff --git a/sprint2Qs b/sprint2Qs new file mode 100644 index 0000000000..a750b91eed --- /dev/null +++ b/sprint2Qs @@ -0,0 +1,34 @@ +Sprint Questions: +Q1. Briefly compare and contrast .forEach & .map (2-3 sentences max) +A1. +Q2. Explain the difference between a callback and a higher order function. +A2. +Q3. What is closure? +A3. +Q4. Describe the four rules of the 'this' keyword. +A4. +Q5. Why do we need super() in an extended class? +A5. +@here These have to be answered for you to make MVP. Please make it readable. Make sure there is a space between the questions and answers. +:+1: +1 + + +Charise Arter:lambdawb: 20:55 +Notes for you on JavaScript +https://lambda-students.slack.com/archives/G016RFVJGQ4/p1597290124112700 +20:55 +https://lambda-students.slack.com/archives/G016RFVJGQ4/p1597290075112200 +20:56 +I have no idea if this will show.... +https://codepen.io/charisearter/pen/PoPzegK +https://codepen.io/charisearter/pen/rNOLvXE +https://codepen.io/charisearter/pen/pojEdbJ +https://codepen.io/charisearter/pen/LYpRmPg +https://codepen.io/charisearter/pen/yLYapeo +https://codepen.io/charisearter/pen/zYvojJL +https://codepen.io/charisearter/pen/YzyNygv +https://codepen.io/charisearter/pen/PoPWgGg +Basics of JAvaScript: https://codepen.io/charisearter/pen/WNQpjLd +Objects & Arrays: https://codepen.io/charisearter/pen/wvKJdNq +Prototypes and Inheritance notes: https://codepen.io/charisearter/pen/ExVWmBB From aaef29b9b1f6eea6c08c121ad4535a5d021e9ebb Mon Sep 17 00:00:00 2001 From: extrude575757 Date: Fri, 14 Aug 2020 19:16:49 -0700 Subject: [PATCH 02/10] Wont work when it should --- challenges/arrays-callbacks.js | 63 +++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 12af878ceb..82f8688265 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -1,6 +1,7 @@ // ==== 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. +// 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" }, @@ -17,24 +18,51 @@ const zooAnimals = [ /* Request 1: .forEach() -The zoos want to display both the scientific name and the animal name in front of the habitats. Populate the displayNames array with only the animal_name and scientific_name of each animal. displayNames will be an array of strings, and each string should follow this pattern: "Name: Jackal, asiatic, Scientific: Canis aureus." +The zoos want to display both the scientific name and the animal name in front of the habitats. + Populate the +displayNames array with only the animal_name and scientific_name of each animal. displayNames will +be an array +of strings, and each string should follow this pattern: "Name: Jackal, asiatic, Scientific: +Canis aureus." */ const displayNames = []; -console.log(displayNames); +let count = 0; + zooAnimals.forEach(function(e){ -/* Request 2: .map() + displayNames[count] = `Name: ${e.animal_name} Scientific: ${e.scientific_name}.`; + ++count; +}); + +console.log('Task1 - Array callbacks'); +displayNames.forEach(e => console.log(e)); -The zoos need a list of all their animal's names (animal_name only) converted to lower case. Using map, create a new array of strings named lowCaseAnimalNames, each string following this pattern: "jackal, asiatic". Log the resut. +/* Request 2: .map() +The zoos need a list of all their animal's names (animal_name only) + converted to lower case. Using map, create +a new array of strings named lowCaseAnimalNames, each string following +this pattern: "jackal, asiatic". Log the + resut. */ +count = 0; +let lowCaseAnimalNames = []; +lowCaseAnimalNames = zooAnimals.map(function(e){ + return e.animal_name.toLowerCase(); +}); + -const lowCaseAnimalNames = []; -console.log(lowCaseAnimalNames); +console.log('Task 2'); +for(let i = 0; i < lowCaseAnimalNames.length; i++){ + console.log(lowCaseAnimalNames[i]); +} /* Request 3: .filter() -The zoos are concerned about animals with a lower population count. Using filter, create a new array of objects called lowPopulationAnimals which contains only the animals with a population less than 5. +The zoos are concerned about animals with a lower population count. +Using filter, create a new array of objects + called lowPopulationAnimals which contains only the animals with a + population less than 5. */ const lowPopulationAnimals = []; @@ -42,7 +70,11 @@ console.log(lowPopulationAnimals); /* Request 4: .reduce() -The zoos need to know their total animal population across the United States. Find the total population from all the zoos using the .reduce() method. Remember the reduce method takes two arguments: a callback (which itself takes two args), and an initial value for the count. +The zoos need to know their total animal population across the United States. +Find the total population from all + the zoos using the .reduce() method. Remember the reduce method takes two + arguments: a callback (which itself + takes two args), and an initial value for the count. */ let populationTotal = 0; @@ -55,28 +87,33 @@ console.log(populationTotal); * Create a higher-order function named consume with 3 parameters: a, b and cb * The first two parameters can take any argument (we can pass any value as argument) * The last parameter accepts a callback - * The consume function should return the invocation of cb, passing a and b into cb as arguments + * The consume function should return the invocation of cb, passing a and b into cb as + * arguments */ /* Step 2: Create several functions to callback with consume(); * Create a function named add that returns the sum of two numbers * Create a function named multiply that returns the product of two numbers - * Create a function named greeting that accepts a first and last name and returns "Hello first-name last-name, nice to meet you!" + * Create a function named greeting that accepts a first and last name and returns + * "Hello first-name last-name, + * nice to meet you!" */ /* 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("Mary", "Poppins", greeting)); // Hello Mary Poppins, nice +//to meet you! /* -Stretch: If you haven't already, convert your array method callbacks into arrow functions. +Stretch: If you haven't already, convert your array method callbacks into arrow +functions. */ From ba7ed2ae203bc6919199daf4817569d29e8c6087 Mon Sep 17 00:00:00 2001 From: extrude575757 Date: Fri, 14 Aug 2020 19:41:48 -0700 Subject: [PATCH 03/10] Callbacks --- challenges/arrays-callbacks.js | 62 +++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 82f8688265..a3412ea5f8 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -4,16 +4,27 @@ // 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" }, + { 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() @@ -65,8 +76,14 @@ Using filter, create a new array of objects population less than 5. */ -const lowPopulationAnimals = []; -console.log(lowPopulationAnimals); +let lowPopulationAnimals = []; + +lowPopulationAnimals = zooAnimals.filter(e => e.population <= 5); + +console.log('Task3'); +for(let i in lowPopulationAnimals){ + console.log(lowPopulationAnimals[i].animal_name); +} /* Request 4: .reduce() @@ -78,7 +95,26 @@ Find the total population from all */ let populationTotal = 0; -console.log(populationTotal); +// Array reducer +const reducer = (accumulator, currentValue) => accumulator + currentValue; +/* Array OBject accumulator +let initialValue = 0 +let sum = [{x: 1}, {x: 2}, {x: 3}].reduce(function (accumulator, currentValue) { + return accumulator + currentValue.x +}, initialValue) + +console.log(sum) // logs 6 +*/ +// population array +let popar = []; +for(let i = 0; i < zooAnimals.length; i++){ + popar.push(zooAnimals[i].population); +} + +for(let i = 0; i< zooAnimals.length; i++){ + console.log('Task 4 Total Popualtion: '+popar.reduce(reducer)); +} + // ==== Callbacks ==== From b2cc89ce9c03d9a902b26c8de4a8a2690e7ca893 Mon Sep 17 00:00:00 2001 From: extrude575757 Date: Fri, 14 Aug 2020 20:15:39 -0700 Subject: [PATCH 04/10] Array callbacks finsihed --- challenges/arrays-callbacks.js | 93 +++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 35 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index a3412ea5f8..bbd1f28fb6 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -4,27 +4,27 @@ // 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" }, + { 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() @@ -38,11 +38,11 @@ Canis aureus." */ const displayNames = []; -let count = 0; - zooAnimals.forEach(function(e){ - - displayNames[count] = `Name: ${e.animal_name} Scientific: ${e.scientific_name}.`; - ++count; + let count = 0; + zooAnimals.forEach(function(e){ +// Add up the object properties + displayNames[count] = `Name: ${e.animal_name} Scientific: ${e.scientific_name}.`; + ++count; }); console.log('Task1 - Array callbacks'); @@ -56,16 +56,17 @@ this pattern: "jackal, asiatic". Log the resut. */ -count = 0; let lowCaseAnimalNames = []; lowCaseAnimalNames = zooAnimals.map(function(e){ - return e.animal_name.toLowerCase(); +// Return the lowercase animals names + return e.animal_name.toLowerCase(); }); console.log('Task 2'); for(let i = 0; i < lowCaseAnimalNames.length; i++){ - console.log(lowCaseAnimalNames[i]); +// Print the lowercase animal names + console.log(lowCaseAnimalNames[i]); } /* Request 3: .filter() @@ -82,7 +83,8 @@ lowPopulationAnimals = zooAnimals.filter(e => e.population <= 5); console.log('Task3'); for(let i in lowPopulationAnimals){ - console.log(lowPopulationAnimals[i].animal_name); +// All low population animals + console.log(lowPopulationAnimals[i].animal_name); } /* Request 4: .reduce() @@ -108,11 +110,13 @@ console.log(sum) // logs 6 // population array let popar = []; for(let i = 0; i < zooAnimals.length; i++){ - popar.push(zooAnimals[i].population); +// Make a population array to use with reducer + popar.push(zooAnimals[i].population); } for(let i = 0; i< zooAnimals.length; i++){ - console.log('Task 4 Total Popualtion: '+popar.reduce(reducer)); +// The population array added up + console.log('Task 4 Total Popualtion: '+popar.reduce(reducer)); } @@ -135,12 +139,31 @@ for(let i = 0; i< zooAnimals.length; i++){ * "Hello first-name last-name, * nice to meet you!" */ +function add(a,b){ + + return a+b; +} +function multiply(a,b){ + + return a*b; +} +function greeting(a,b){ + + return `Hello ${a} ${b}, nice to meet you!`; +} + +function consume(a,b,cb){ + return cb(a,b); + + } + //console.log(consume(3,3,cconsume())); +console.log('Consume callback functions '); /* 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 +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! From c01d794e6f8426f97e4fd84eb94973c2e001a12e Mon Sep 17 00:00:00 2001 From: extrude575757 Date: Fri, 14 Aug 2020 20:44:58 -0700 Subject: [PATCH 05/10] Closure --- README.md | 24 ++++++++++++++++++++++++ challenges/closure.js | 21 +++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cc6d6902fb..050fa79e0c 100644 --- a/README.md +++ b/README.md @@ -26,14 +26,38 @@ Edit this document to include your answers after each question. Make sure to lea 1. Briefly compare and contrast `.forEach` & `.map` (2-3 sentences max) + Both are similar however .map returns a new array with transformed + elements while .forEach returns undefined. Also .map is chainable + while .forEach is not. This means .map can handle other calls like + .sort or .reduce after the .map call. This is something .forEach + is not capable of. Also both methods do not mutate the array's + elements on its own without a callback function in use to do so. + + 2. Explain the difference between a callback and a higher order function. + A higher order function is the outer scope block within the function + a global scope could call. However a callback function can not be + invoked or called by the global scope. Only the higher order function's + scope is capable of accessing the callback function. + 3. What is closure? + + Closure is a scope block a variable or function can be accessable within. + Depending on how many nested curly braces there are closure can be increased + or decreased. 4. Describe the four rules of the 'this' keyword. + + 5. Why do we need super() in an extended class? + Super in a extended class will allow the parent class to be accessible from + the child class. So all properties a parent class have the child class will + as well by just adding in the paramerters need within the parent construtor + through the super function inside of the child class. + ### Task 1 - Project Set up Follow these steps to set up and work on your project: diff --git a/challenges/closure.js b/challenges/closure.js index 101d68e553..1d14a1536c 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -1,6 +1,7 @@ // ==== Closures ==== -/* Task 1: Study the code below and explain in your own words why nested function can access the variable internal. */ +/* Task 1: Study the code below and explain in your own words why nested +function can access the variable internal. */ const external = "I'm outside the function"; @@ -17,8 +18,24 @@ function myFunction() { myFunction(); // Explanation: +/* + The reason internal is accessbile by nested function is the same type of + reason external is accessible within the myFunction. Its because of scope block + The inner nested scope blocks allow these variables closure to those functions. +*/ /* Task 2: Counter */ -/* Create a function called `sumation` that accepts a parameter and uses a counter to return the summation of that number. For example, `summation(4)` should return 10 because 1+2+3+4 is 10. */ +/* Create a function called `sumation` that accepts a parameter and uses + a counter to return the summation of that number. For example, + `summation(4)` should return 10 because 1+2+3+4 is 10. */ +const sumation = (p) => { + let result = 0; + for(let counter = 0; counter <= p; counter++){ + result = result + counter; + } + return result; +}; +console.log('summation '+ sumation(4)); + From f6eae4f15f6675d299bd9889981d6928746db165 Mon Sep 17 00:00:00 2001 From: extrude575757 Date: Fri, 14 Aug 2020 21:32:27 -0700 Subject: [PATCH 06/10] Finished with prototypes --- challenges/arrays-callbacks.js | 13 +++++++++ challenges/prototypes.js | 41 +++++++++++++++++++++------ sprint2Qs | 52 +++++++++++++++++++++++++--------- 3 files changed, 83 insertions(+), 23 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index bbd1f28fb6..f82e3a52e4 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -176,3 +176,16 @@ functions. */ + + +/* + Call back function for buttons clickes + +This time we will see a message on the console only when the user clicks on the button: + +document.queryselector("#callback-btn") + .addEventListener("click", function() { + console.log("User has clicked on the button!"); +}); + +*/ diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..023ca0d450 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -1,33 +1,56 @@ /* ===== Prototype Practice ===== */ -// 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. +// 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. /* == Step 1: Base Constructor == - Create a constructor function named CuboidMaker that accepts properties for length, width, and height + Create a constructor function named CuboidMaker that accepts properties for length, + width, and height */ +function CuboidMaker(length,width,height){ + this.length = length; + this.width = width; + this.height = height; +} + + /* == Step 2: Volume Method == - Create a method using CuboidMaker's prototype that returns the volume of a given cuboid's length, width, and height + Create a method using CuboidMaker's prototype that returns the volume of a given + cuboid's length, width, and height 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 using CuboidMaker's prototype that returns the surface area of a given cuboid's length, width, and height. + Create another method using CuboidMaker's prototype that returns the surface area + of a given cuboid's length, width, and height. - Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height) + Formula for cuboid surface area of a cube: 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 == Create a cuboid object that uses the new keyword to use our CuboidMaker constructor Add properties and values of length: 4, width: 5, and height: 5 to cuboid. */ + + +let cuboid = new CuboidMaker(4,5,5); + +console.log('Step 4 of Prototypes '); // 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 diff --git a/sprint2Qs b/sprint2Qs index a750b91eed..e032d9debe 100644 --- a/sprint2Qs +++ b/sprint2Qs @@ -1,17 +1,41 @@ -Sprint Questions: -Q1. Briefly compare and contrast .forEach & .map (2-3 sentences max) -A1. -Q2. Explain the difference between a callback and a higher order function. -A2. -Q3. What is closure? -A3. -Q4. Describe the four rules of the 'this' keyword. -A4. -Q5. Why do we need super() in an extended class? -A5. -@here These have to be answered for you to make MVP. Please make it readable. Make sure there is a space between the questions and answers. -:+1: -1 + + +1. Briefly compare and contrast `.forEach` & `.map` (2-3 sentences max) + + Both are similar however .map returns a new array with transformed + elements while .forEach returns undefined. Also .map is chainable + while .forEach is not. This means .map can handle other calls like + .sort or .reduce after the .map call. This is something .forEach + is not capable of. Also both methods do not mutate the array's + elements on its own without a callback function in use to do so. + + +2. Explain the difference between a callback and a higher order function. + + A higher order function is the outer scope block within the function + a global scope could call. However a callback function can not be + invoked or called by the global scope. Only the higher order function's + scope is capable of accessing the callback function. + +3. What is closure? + + Closure is a scope block a variable or function can be accessable within. + Depending on how many nested curly braces there are closure can be increased + or decreased. + +4. Describe the four rules of the 'this' keyword. + That it will refer to a object's implict value set within the constructor. + + + + +5. Why do we need super() in an extended class? + + Super in a extended class will allow the parent class to be accessible from + the child class. So all properties a parent class have the child class will + as well by just adding in the paramerters need within the parent construtor + through the super function inside of the child class. + Charise Arter:lambdawb: 20:55 From 9269091ba18bcc1f8c68d949435e6b6377954935 Mon Sep 17 00:00:00 2001 From: extrude575757 Date: Fri, 14 Aug 2020 21:44:46 -0700 Subject: [PATCH 07/10] Onto Stretch goals --- challenges/classes.js | 38 ++++++++++++++++++++++++++++++++++---- challenges/prototypes.js | 2 +- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..7cfedecd30 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,37 @@ -// 1. Copy and paste your prototype in here and refactor into class syntax. +// 1. Copy and paste your prototype in here and refactor into +//class syntax. + + +class CubeMaker{ + constructor(length,width,height){ + this.length = length; + this.height = height; + this.width = width; + } + + volume(){ + return this.length * this.width * this.height; + } + + surfaceArea(){ + return 2 * (this.length * this.width + this.length * this.height + + this.width * this.height); + } +} + + +console.log('Cklasses '); + +//let c = new CubeMaker(55,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 + -// Stretch Task: Extend the base class CuboidMaker with a sub class called CubeMaker. Find out the formulas for volume and surface area for cubes and create those methods using the dimension properties from CuboidMaker. Test your work by logging out your volume and surface area. \ No newline at end of file +// console.log(c.volume()); // 100 +// console.log(c.surfaceArea()); // 130 +// Stretch Task: Extend the base class CuboidMaker with a sub class +//called CubeMaker. Find out the formulas for volume and surface area for +// cubes and create those methods using the dimension properties +//from CuboidMaker. Test your work by logging out your volume and surface area. \ No newline at end of file diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 023ca0d450..4bd91c2cb7 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -36,7 +36,7 @@ CuboidMaker.prototype.volume = function(){ */ CuboidMaker.prototype.surfaceArea = function(){ return 2 * (this.length * this.width + this.length * this.height + - this.width * this.height) + this.width * this.height); } /* == Step 4: Create a new object that uses CuboidMaker == From 7015ac774f2a50714247039901a0f510c75a8db6 Mon Sep 17 00:00:00 2001 From: extrude575757 Date: Fri, 14 Aug 2020 22:10:45 -0700 Subject: [PATCH 08/10] The questions --- sprint2Qs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/sprint2Qs b/sprint2Qs index e032d9debe..ee4a92afb3 100644 --- a/sprint2Qs +++ b/sprint2Qs @@ -24,8 +24,27 @@ or decreased. 4. Describe the four rules of the 'this' keyword. - That it will refer to a object's implict value set within the constructor. - + +1. Simple Function call: Where global functions defaults to the global object and in + strict mode they will default to undefined + +2. Implicit value: The Implicit value points to the object from which it is called + and accounts for the majority of day to day code examples of this. This as an + Implicit value is what is to the left of the assignment operator. Which is used + when a constructor's value is referenced. + + +3. Explicti Value: Used when assigning a specific function or variable a value + for Prototypes bind apply or call all use this as a explicit value. In classes + this is similar to passing in values throug the super keyword or when assigning + a function to a variable to return an explicitvalue as the bind functions does. + + +4. new binding: Using the new keyword constructs a new object and this points to it. + So when a function is invoked as a constructor function the this will then point + to the new object created to the left of the assignment operator. + + From 5c8726e913a8cbc201cc93da1b75bbd34866ce1a Mon Sep 17 00:00:00 2001 From: extrude575757 Date: Fri, 14 Aug 2020 22:38:50 -0700 Subject: [PATCH 09/10] Stretch goal --- README.md | 26 ++++++++++++++++++++++++++ challenges/classes.js | 29 ++++++++++++++++++++++++++--- challenges/index.html | 7 ++++++- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 050fa79e0c..71554dc8f8 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ Demonstrate your understanding of this week's concepts by answering the followin Edit this document to include your answers after each question. Make sure to leave a blank line above and below your answer so it is clear and easy to read by your team lead + + 1. Briefly compare and contrast `.forEach` & `.map` (2-3 sentences max) Both are similar however .map returns a new array with transformed @@ -49,6 +51,27 @@ Edit this document to include your answers after each question. Make sure to lea 4. Describe the four rules of the 'this' keyword. +1. Simple Function call: Where global functions defaults to the global object and in + strict mode they will default to undefined + +2. Implicit value: The Implicit value points to the object from which it is called + and accounts for the majority of day to day code examples of this. This as an + Implicit value is what is to the left of the assignment operator. Which is used + when a constructor's value is referenced. + + +3. Explicti Value: Used when assigning a specific function or variable a value + for Prototypes bind apply or call all use this as a explicit value. In classes + this is similar to passing in values throug the super keyword or when assigning + a function to a variable to return an explicitvalue as the bind functions does. + + +4. new binding: Using the new keyword constructs a new object and this points to it. + So when a function is invoked as a constructor function the this will then point + to the new object created to the left of the assignment operator. + + + 5. Why do we need super() in an extended class? @@ -58,6 +81,9 @@ Edit this document to include your answers after each question. Make sure to lea as well by just adding in the paramerters need within the parent construtor through the super function inside of the child class. + + + ### Task 1 - Project Set up Follow these steps to set up and work on your project: diff --git a/challenges/classes.js b/challenges/classes.js index 7cfedecd30..9aafa850c1 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -2,7 +2,7 @@ //class syntax. -class CubeMaker{ +class CuboidMaker{ constructor(length,width,height){ this.length = length; this.height = height; @@ -22,7 +22,7 @@ class CubeMaker{ console.log('Cklasses '); -//let c = new CubeMaker(55,5,5); +let cuboid = new CuboidMaker(5,5,4); // Test your volume and surfaceArea methods by uncommenting the logs below: console.log(cuboid.volume()); // 100 @@ -31,7 +31,30 @@ console.log(cuboid.surfaceArea()); // 130 // console.log(c.volume()); // 100 // console.log(c.surfaceArea()); // 130 + // Stretch Task: Extend the base class CuboidMaker with a sub class //called CubeMaker. Find out the formulas for volume and surface area for // cubes and create those methods using the dimension properties -//from CuboidMaker. Test your work by logging out your volume and surface area. \ No newline at end of file +//from CuboidMaker. Test your work by logging out your volume and surface +//area. + +class CubeMaker extends CuboidMaker{ + constructor(a,b,c){ + super(a,b,c); + this.cubeMakerValue = 0; + } + makeAcube(c,d){ + this.cubeMakerValue = c * d; + } + theCube(){ + return this.cubeMakerValue; + } +} +let cube = new CubeMaker(5,5,4); +console.log('With the strech goal'); +console.log('Volume '+cube.volume()); // 100 +console.log('Surface Area '+cube.surfaceArea()); // 130 +cube.makeAcube(cube.volume(),cube.surfaceArea()); +console.log('The cube with surface area and volume '+ cube.theCube()); + + diff --git a/challenges/index.html b/challenges/index.html index 416db8e1d0..be3b51c331 100644 --- a/challenges/index.html +++ b/challenges/index.html @@ -9,8 +9,13 @@ - + From 9236c5aedb2a7f52125579527c03be7a92e6ea6f Mon Sep 17 00:00:00 2001 From: extrude575757 Date: Sun, 16 Aug 2020 00:49:16 -0700 Subject: [PATCH 10/10] Stretch goals completed by the arrow function add on --- challenges/arrays-callbacks.js | 15 ++++++++------- challenges/classes.js | 8 ++++---- challenges/closure.js | 4 ++-- challenges/index.html | 3 ++- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index f82e3a52e4..a23872365f 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -35,11 +35,12 @@ displayNames array with only the animal_name and scientific_name of each animal. be an array of strings, and each string should follow this pattern: "Name: Jackal, asiatic, Scientific: Canis aureus." - +Stretch goal +Convert the regular functions into new es6 arrow functions */ const displayNames = []; let count = 0; - zooAnimals.forEach(function(e){ + zooAnimals.forEach((e) =>{ // Add up the object properties displayNames[count] = `Name: ${e.animal_name} Scientific: ${e.scientific_name}.`; ++count; @@ -57,7 +58,7 @@ this pattern: "jackal, asiatic". Log the */ let lowCaseAnimalNames = []; -lowCaseAnimalNames = zooAnimals.map(function(e){ +lowCaseAnimalNames = zooAnimals.map((e)=>{ // Return the lowercase animals names return e.animal_name.toLowerCase(); }); @@ -139,21 +140,21 @@ for(let i = 0; i< zooAnimals.length; i++){ * "Hello first-name last-name, * nice to meet you!" */ -function add(a,b){ + add = (a,b) => { return a+b; } -function multiply(a,b){ + multiply=(a,b)=>{ return a*b; } -function greeting(a,b){ + greeting=(a,b)=>{ return `Hello ${a} ${b}, nice to meet you!`; } -function consume(a,b,cb){ + consume=(a,b,cb)=>{ return cb(a,b); diff --git a/challenges/classes.js b/challenges/classes.js index 9aafa850c1..c00b941a24 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -9,11 +9,11 @@ class CuboidMaker{ this.width = width; } - volume(){ + volume = () =>{ return this.length * this.width * this.height; } - surfaceArea(){ + surfaceArea = () =>{ return 2 * (this.length * this.width + this.length * this.height + this.width * this.height); } @@ -43,10 +43,10 @@ class CubeMaker extends CuboidMaker{ super(a,b,c); this.cubeMakerValue = 0; } - makeAcube(c,d){ + makeAcube = (c,d) =>{ this.cubeMakerValue = c * d; } - theCube(){ + theCube = () =>{ return this.cubeMakerValue; } } diff --git a/challenges/closure.js b/challenges/closure.js index 1d14a1536c..a91bec515a 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -6,11 +6,11 @@ function can access the variable internal. */ const external = "I'm outside the function"; -function myFunction() { + myFunction = () => { console.log(external); const internal = "Hello! I'm inside myFunction!"; - function nestedFunction() { + nestedFunction=()=> { console.log(internal); }; nestedFunction(); diff --git a/challenges/index.html b/challenges/index.html index be3b51c331..492a45128f 100644 --- a/challenges/index.html +++ b/challenges/index.html @@ -12,7 +12,8 @@