From 6812eec8a904c266de0f7013ee306d48e3688417 Mon Sep 17 00:00:00 2001 From: Alex Vargas Date: Fri, 22 May 2020 13:52:56 -0400 Subject: [PATCH 1/3] need to finish callbacks and closure --- README.md | 9 +++++++-- challenges/arrays-callbacks.js | 9 ++++++--- challenges/classes.js | 10 +++++++--- challenges/index.html | 2 +- challenges/prototypes.js | 22 ++++++++++++++++------ 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index cc6d6902fb..beec6a6aee 100644 --- a/README.md +++ b/README.md @@ -25,14 +25,19 @@ 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) +The .forEach modify the array. The .map creates a new array. 2. Explain the difference between a callback and a higher order function. -3. What is closure? +callbacks are passed into higher order functions. While +higher order functions receive callbacks as arguments. +3. What is closure? +Closure reaches outside of a function if its scope doesn't have a variable that is define. 4. Describe the four rules of the 'this' keyword. - +The this keyword to reference an object without having to refer to that object’s name. I like to personaly think of the word dude. 5. Why do we need super() in an extended class? +to bind classes to together to achieve some simple object inheritance ### Task 1 - Project Set up diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 472ab3e96d..7fcc6759df 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -21,6 +21,7 @@ The zoos want to display both the scientific name and the animal name in front o */ const displayNames = []; +zooAnimals.forEach( arrItem => {displayNames.push(`Name: ${arrItem.animal_name} Scietific : ${arrItem.scientific_name}.`)}) console.log(displayNames); /* Request 2: .map() @@ -29,7 +30,7 @@ The zoos need a list of all their animal's names (animal_name only) converted to */ -const lowCaseAnimalNames = []; +const lowCaseAnimalNames = zooAnimals.map(arrItem => arrItem.animal_name.toLowerCase());; console.log(lowCaseAnimalNames); /* Request 3: .filter() @@ -37,7 +38,7 @@ console.log(lowCaseAnimalNames); 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 = []; +const lowPopulationAnimals = zooAnimals.filter(arrItem => arrItem.population <5); console.log(lowPopulationAnimals); /* Request 4: .reduce() @@ -45,7 +46,9 @@ console.log(lowPopulationAnimals); 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. */ -const populationTotal = 0; +const populationTotal = zooAnimals.reduce((acc, animal)=> +{ return acc + animal.population; +},0); console.log(populationTotal); diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..7aa1153ec6 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,11 @@ // 1. Copy and paste your prototype in here and refactor into class syntax. - +function CuboidMaker(length, width, height){ + this.length = length; + this.width = width; + this.height = height; +}; // 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 diff --git a/challenges/index.html b/challenges/index.html index 5772804b7f..91b64af080 100644 --- a/challenges/index.html +++ b/challenges/index.html @@ -7,7 +7,7 @@ Sprint Challenge - + diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..43294941f3 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -5,29 +5,39 @@ /* == Step 1: Base Constructor == Create a constructor function named CuboidMaker that accepts properties for length, width, and height */ - +const cuboid = new CuboidMaker (4, 5, 5); +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 Formula for cuboid volume: length * width * height */ - +CuboidMaker.prototype.volume = function () { + return (this.lenght * 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. Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height) */ - +CuboidMaker.prototype.surfaceArea = Function () { + return (2 * (this.lenght * this.width + + this.height * this.lenght + 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. */ - + 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 From 2ce75610ab2ec7dac291732b70187a832c87ac37 Mon Sep 17 00:00:00 2001 From: Alex Vargas Date: Fri, 22 May 2020 18:41:33 -0400 Subject: [PATCH 2/3] Complete --- challenges/arrays-callbacks.js | 35 +++++++++++++++++++++++++++++++--- challenges/classes.js | 2 +- challenges/closure.js | 8 ++++++++ challenges/prototypes.js | 5 ++--- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 7fcc6759df..3a357efe80 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -60,6 +60,10 @@ console.log(populationTotal); * The last parameter accepts a callback * The consume function should return the invocation of cb, passing a and b into cb as arguments */ +function highOrder (cb, a, b){ + return cb(a, b); +} + /* Step 2: Create several functions to callback with consume(); @@ -67,12 +71,37 @@ console.log(populationTotal); * 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!" */ +function add(num1, num2){ + return num1 + num2; +} + +function multiply(mu1,mu2){ + return mu1 * mu2 + } + + + function greeting(first,last){ + return `Hello ${first} ${last}, nice to meet you!` + } + + +function consume(a, b, type){ +if(type === 'add'){ + add(a,b)}; + if(type === 'multiply'){ + multiply(a,b)}; + if(type === 'greeting'){ + greeting(a,b)}; + + + +} /* 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! diff --git a/challenges/classes.js b/challenges/classes.js index 7aa1153ec6..285570a0c4 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,5 +1,5 @@ // 1. Copy and paste your prototype in here and refactor into class syntax. -function CuboidMaker(length, width, height){ +function CuboidMaker1(length, width, height){ this.length = length; this.width = width; this.height = height; diff --git a/challenges/closure.js b/challenges/closure.js index 101d68e553..691042da7d 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -20,5 +20,13 @@ myFunction(); /* Task 2: Counter */ +let cou = 4; +function sumation (cou) { + for(let i = 0; i <= cou; i++){ + cou++; +} + +} +console.log(sumation(4)) /* 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. */ diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 43294941f3..78305fc490 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -26,9 +26,8 @@ CuboidMaker.prototype.volume = function () { Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height) */ -CuboidMaker.prototype.surfaceArea = Function () { - return (2 * (this.lenght * this.width + - this.height * this.lenght + this.width * this.height)) +CuboidMaker.prototype.surfaceArea = function () { + return (2 * (this.lenght * this.width + this.height * this.lenght + this.width * this.height)) } /* == Step 4: Create a new object that uses CuboidMaker == From 80e7049f5d45a407de7f95ce53ccfd2e20cedf78 Mon Sep 17 00:00:00 2001 From: Alex Vargas Date: Tue, 26 May 2020 12:41:27 -0400 Subject: [PATCH 3/3] review by vick --- challenges/arrays-callbacks.js | 10 +++------- challenges/classes.js | 23 ++++++++++++++++++----- challenges/closure.js | 4 ++-- challenges/prototypes.js | 6 +++--- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 3a357efe80..bc5ab1ba9f 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -85,13 +85,9 @@ function multiply(mu1,mu2){ } -function consume(a, b, type){ -if(type === 'add'){ - add(a,b)}; - if(type === 'multiply'){ - multiply(a,b)}; - if(type === 'greeting'){ - greeting(a,b)}; +function consume(a, b, func){ +return func(a,b); + diff --git a/challenges/classes.js b/challenges/classes.js index 285570a0c4..10d7742e82 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,11 +1,24 @@ // 1. Copy and paste your prototype in here and refactor into class syntax. -function CuboidMaker1(length, width, height){ +class CuboidMaker1{ + constructor (length, width, height){ this.length = length; this.width = width; this.height = height; -}; + } +// this.volume = lenght * width * height; +// this.surfaceArea = length * width + length * height + width * height; + // Test your volume and surfaceArea methods by uncommenting the logs below: -console.log(cuboid.volume()); // 100 -console.log(cuboid.surfaceArea()); // 130 + vol(){ + return this.length * this.width * this.height; +}; + +sur(){ + return (2 * (this.length * this.width + this.height * this.length + this.width * this.height)) +};}; + -// 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 +const cuboidthingy = new CuboidMaker1 (4, 5, 5); +// 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. +console.log(cuboidthingy.vol()); // 100 +console.log(cuboidthingy.sur()); // 130 \ No newline at end of file diff --git a/challenges/closure.js b/challenges/closure.js index 691042da7d..77b2843290 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -16,9 +16,9 @@ function myFunction() { } myFunction(); -// Explanation: - +// Explanation: the reason why because of scoping. Scope feature starts from the variable being called on. If its not there thn it wil go outside of the function to look for it. + /* Task 2: Counter */ let cou = 4; diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 78305fc490..4407a0d71e 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -5,7 +5,7 @@ /* == Step 1: Base Constructor == Create a constructor function named CuboidMaker that accepts properties for length, width, and height */ -const cuboid = new CuboidMaker (4, 5, 5); + function CuboidMaker(length, width, height){ this.length =length; this.width = width; @@ -18,7 +18,7 @@ function CuboidMaker(length, width, height){ Formula for cuboid volume: length * width * height */ CuboidMaker.prototype.volume = function () { - return (this.lenght * this.width * this.height); + return (this.length * this.width * this.height); }; /* == Step 3: Surface Area Method == @@ -27,7 +27,7 @@ CuboidMaker.prototype.volume = function () { Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height) */ CuboidMaker.prototype.surfaceArea = function () { - return (2 * (this.lenght * this.width + this.height * this.lenght + this.width * this.height)) + return (2 * (this.length * this.width + this.height * this.length + this.width * this.height)) } /* == Step 4: Create a new object that uses CuboidMaker ==