From d76f409420bf39aa26078107e5ce9d88edeeac21 Mon Sep 17 00:00:00 2001 From: Terry Edwards Date: Sat, 16 May 2020 13:18:29 -0400 Subject: [PATCH 1/5] halfway done --- challenges/arrays-callbacks.js | 23 +++++++++++++++++------ challenges/closure.js | 8 ++++++++ challenges/prototypes.js | 28 +++++++++++++++++++++++----- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 472ab3e96d..1255dc48b2 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -21,6 +21,8 @@ The zoos want to display both the scientific name and the animal name in front o */ const displayNames = []; +let display=zooAnimals.forEach((displays)=>{ + displayNames.push(`Name: ${displays.animal_name}, Scientific: ${displays.scientific_name}`)}) console.log(displayNames); /* Request 2: .map() @@ -30,6 +32,7 @@ The zoos need a list of all their animal's names (animal_name only) converted to */ const lowCaseAnimalNames = []; +let lowAnimals=zooAnimals.map((names)=>lowCaseAnimalNames.push(names.animal_name)) console.log(lowCaseAnimalNames); /* Request 3: .filter() @@ -38,6 +41,7 @@ The zoos are concerned about animals with a lower population count. Using filter */ const lowPopulationAnimals = []; +let lowPop=zooAnimals.filter((pop)=>{if(pop.population<5){lowPopulationAnimals.push(`Name- ${pop.animal_name}, Population- ${pop.population}`)}}) console.log(lowPopulationAnimals); /* Request 4: .reduce() @@ -45,8 +49,8 @@ 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; -console.log(populationTotal); +const populationTotal=zooAnimals.reduce((total,finalPop)=>{return total+=finalPop.population;},0) +console.log("The total animal population across the United States is "+populationTotal+" animals!"); // ==== Callbacks ==== @@ -57,7 +61,9 @@ 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 consume(a,b,cb){ + return cb(a,b) +}; /* Step 2: Create several functions to callback with consume(); * Create a function named add that returns the sum of two numbers @@ -65,11 +71,16 @@ console.log(populationTotal); * 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(a,b,cb){return(a+b)} + +function multiply(a,b,cb){return(a*b)} + +function greeting(a,b,cb){return(`Hello ${a} ${b}, 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(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/closure.js b/challenges/closure.js index 101d68e553..10f3833401 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -22,3 +22,11 @@ myFunction(); /* 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. */ +function sumation(x){ + let sum=0; + for(let i=1;i<=x;i++){ + sum+=i; + } + return(sum) +} +console.log(sumation(4)); \ No newline at end of file diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..71c93c34de 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -5,13 +5,19 @@ /* == Step 1: Base Constructor == 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 Formula for cuboid volume: length * width * height -*/ +*/CuboidMaker.prototype.volume=function(){ + return(this.length*this.width*this.height); +} /* == Step 3: Surface Area Method == @@ -19,15 +25,27 @@ 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. */ - +// function Child(length,width,height){ +// CuboidMaker.call(this,length,width,height); +// } +// Child.prototype=Object.create(CuboidMaker.prototype) +// const cube = new Child({ +// length:4, width:5, height:5, +// }); +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 9dc003d01396627a9926d4c087e403ec02c88b65 Mon Sep 17 00:00:00 2001 From: Terry Edwards Date: Sat, 16 May 2020 13:44:53 -0400 Subject: [PATCH 2/5] one left --- challenges/classes.js | 15 +++++++++++++++ challenges/prototypes.js | 4 +--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..2e62a733d2 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,5 +1,20 @@ // 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; + } + + CuboidMaker.prototype.volume=function(){ + return(this.length*this.width*this.height); + } + CuboidMaker.prototype.surfaceArea=function(){ + return(2 * (this.length * this.width + this.length * this.height + this.width * this.height)) + } + + const cuboid= new CuboidMaker(length=4, width=5, height=5); + // Test your volume and surfaceArea methods by uncommenting the logs below: // console.log(cuboid.volume()); // 100 // console.log(cuboid.surfaceArea()); // 130 diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 71c93c34de..908321d874 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -41,9 +41,7 @@ CuboidMaker.prototype.surfaceArea=function(){ // const cube = new Child({ // length:4, width:5, height:5, // }); -const cuboid=new CuboidMaker({ - 4, 5, 5 - }) +const cuboid= new CuboidMaker(length=4, width=5, height=5); // Test your volume and surfaceArea methods by uncommenting the logs below: console.log(cuboid.volume()); // 100 console.log(cuboid.surfaceArea()); // 130 From dd3f85f3826ca6a9f40e76a3a1ea8b04802aeb61 Mon Sep 17 00:00:00 2001 From: Terry Edwards Date: Sat, 16 May 2020 14:07:15 -0400 Subject: [PATCH 3/5] gotta answer questions still --- challenges/classes.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/challenges/classes.js b/challenges/classes.js index 2e62a733d2..e012b840df 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,22 +1,26 @@ // 1. Copy and paste your prototype in here and refactor into class syntax. -function CuboidMaker(length,width,height){ +class CuboidMaker{ + constructor(length,width,height){ this.length=length; this.width=width; this.height=height; } - CuboidMaker.prototype.volume=function(){ + volume(){ return(this.length*this.width*this.height); } - CuboidMaker.prototype.surfaceArea=function(){ + surfaceArea(){ return(2 * (this.length * this.width + this.length * this.height + this.width * this.height)) } - - const cuboid= new CuboidMaker(length=4, width=5, height=5); - + } + class Cube extends CuboidMaker{ + constructor(length, width, height){ + super(length, width, height); + }} + const cuboid=new Cube(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 // 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 59bb5ffb90f6c9d844f207a462e69f29963e3f08 Mon Sep 17 00:00:00 2001 From: Terry Edwards Date: Sat, 16 May 2020 14:54:52 -0400 Subject: [PATCH 4/5] need to turn map to lowercase --- README.md | 13 +++++++++++++ challenges/arrays-callbacks.js | 2 +- challenges/closure.js | 1 + 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cc6d6902fb..f74617a5bc 100644 --- a/README.md +++ b/README.md @@ -26,14 +26,27 @@ 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) +.forEach and .map are both iterator functions that return arrays. While .forEach is mutable .map is imutable and returns a new array + 2. Explain the difference between a callback and a higher order function. +A higher order function takes another function as an argument and returns a function to its callers but a callback passes itself to another function to be called upon by the said function + 3. What is closure? +Closures give access to the outer scope of a function from the inner function. + 4. Describe the four rules of the 'this' keyword. + 1. the global object binds "this" to the window object. + 2. implicit binding is when "this" refers to whats to the left of the dot when the dot calls a function. + 3. new binding binds "this" to the instance of the object thats being created and returned by a constructor function. + 4. explicit binding is when "this" is explicitly defined to bind "this" to an object by calling the constuctor object with .call and .apply. + 5. Why do we need super() in an extended class? +Super() in an extended class allow binding to acheive object inheritance by telling a parent constructor to be concerned with the child’s attributes. + ### Task 1 - Project Set up Follow these steps to set up and work on your project: diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 1255dc48b2..85a3d313fe 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -34,7 +34,7 @@ The zoos need a list of all their animal's names (animal_name only) converted to const lowCaseAnimalNames = []; let lowAnimals=zooAnimals.map((names)=>lowCaseAnimalNames.push(names.animal_name)) console.log(lowCaseAnimalNames); - +// .toLowercase /* 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. diff --git a/challenges/closure.js b/challenges/closure.js index 10f3833401..e6c6e6d965 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -18,6 +18,7 @@ myFunction(); // Explanation: +// The nested function can access the variable "internal" because they are both inside the "myFunction scope" and while you can't access a variable that is inside a closed function the function can pull variables from outside its scope. /* Task 2: Counter */ From d30c6ec5073e260baf2aa1254a5d42c960cd81e0 Mon Sep 17 00:00:00 2001 From: Terry Edwards Date: Sat, 16 May 2020 15:05:34 -0400 Subject: [PATCH 5/5] FINISHED --- challenges/arrays-callbacks.js | 4 ++-- challenges/closure.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 85a3d313fe..051631128b 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -32,9 +32,9 @@ The zoos need a list of all their animal's names (animal_name only) converted to */ const lowCaseAnimalNames = []; -let lowAnimals=zooAnimals.map((names)=>lowCaseAnimalNames.push(names.animal_name)) +let lowAnimals=zooAnimals.map((names)=>lowCaseAnimalNames.push(names.animal_name.toLowerCase())) console.log(lowCaseAnimalNames); -// .toLowercase + /* 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. diff --git a/challenges/closure.js b/challenges/closure.js index e6c6e6d965..1b0bacef4d 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -18,7 +18,7 @@ myFunction(); // Explanation: -// The nested function can access the variable "internal" because they are both inside the "myFunction scope" and while you can't access a variable that is inside a closed function the function can pull variables from outside its scope. +"The nested function can access the variable (internal) because they are both inside the (myFunction scope) and while you can't access a variable that is inside a closed function the function can pull variables from outside its scope." /* Task 2: Counter */