From fcbf8f320f8efad041c35ead7427c16ce0dce808 Mon Sep 17 00:00:00 2001 From: User Date: Fri, 15 May 2020 21:13:35 -0500 Subject: [PATCH 1/7] Let's Try --- challenges/arrays-callbacks.js | 44 ++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 472ab3e96d..96b40fa3c2 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,36 +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 = []; +displayNames.forEach(element => console.log(element)); console.log(displayNames); /* 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. +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. */ -const lowCaseAnimalNames = []; +const lowCaseAnimalNames = zooAnimals.map(animal_name) { +} +return {"animal_name": animal.lowCaseAnimalNames} console.log(lowCaseAnimalNames); /* 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 = []; +for (let index = 0; index < 5; index++) { + if(data[i].population >= 5) { + lowPopulationAnimals.push(data[i]); 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. */ -const populationTotal = 0; -console.log(populationTotal); +const populationTotal = zooAnimals.reduce((callback (accumulator, 0)) +console.log(populationTotal) // ==== Callbacks ==== @@ -56,15 +72,23 @@ console.log(populationTotal); * 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 -*/ + function consume(cb, b, a) + 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 * 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 (cb) +return sum (a+b); + +function multiply (cb) +return a*b; +function greeting (cb) +return "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 From 244bddcc850afe2d23e7d35f0812160d5ec58c2b Mon Sep 17 00:00:00 2001 From: User Date: Sat, 16 May 2020 12:33:06 -0500 Subject: [PATCH 2/7] Getting Closer --- challenges/arrays-callbacks.js | 6 ++++-- challenges/closure.js | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 96b40fa3c2..16ad4f5200 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -88,7 +88,7 @@ function multiply (cb) return a*b; function greeting (cb) -return "Hello ${first-name} ${last-name}, nice to meet you" +return '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 @@ -96,7 +96,9 @@ return "Hello ${first-name} ${last-name}, nice to meet you" // 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..4e3d9aaefb 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"; @@ -16,9 +17,16 @@ function myFunction() { } myFunction(); -// Explanation: - - +//Explanation: +//Because it is inside the function rather than outside the function. /* 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. */ + +function summation () + { let add = (function () { + var counter = 0; + return function () {counter += 1; return counter} +})(); +} \ No newline at end of file From 762127b83f71e44d34409d8d629f1fcbe43cfebe Mon Sep 17 00:00:00 2001 From: User Date: Sat, 16 May 2020 13:35:17 -0500 Subject: [PATCH 3/7] Objects --- challenges/classes.js | 2 +- challenges/prototypes.js | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..f743e5dfbe 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -4,4 +4,4 @@ // 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 +// 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.z \ No newline at end of file diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..3038a4aade 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -5,22 +5,28 @@ /* == 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 - + 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 */ - +method1.prototype.volume = length * width * 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) */ - - +method2.prototype.surfaceArea = 2 * (length * width + length * height + width * 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. @@ -29,5 +35,4 @@ // Test your volume and surfaceArea methods by uncommenting the logs below: // console.log(cuboid.volume()); // 100 // console.log(cuboid.surfaceArea()); // 130 - - +} From 4ca995a955456238a60e893611b10fb93a8bd59a Mon Sep 17 00:00:00 2001 From: User Date: Sun, 17 May 2020 11:18:51 -0500 Subject: [PATCH 4/7] readme updated --- challenges/prototypes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 3038a4aade..22597c4d86 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -31,7 +31,7 @@ method2.prototype.surfaceArea = 2 * (length * width + length * height + width * 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 CuboidMaker = new Object(); // Test your volume and surfaceArea methods by uncommenting the logs below: // console.log(cuboid.volume()); // 100 // console.log(cuboid.surfaceArea()); // 130 From e7e5a6d1e288614afc431ebdab2795181524f7bb Mon Sep 17 00:00:00 2001 From: User Date: Sun, 17 May 2020 11:21:32 -0500 Subject: [PATCH 5/7] Readme --- README.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cc6d6902fb..99970f604a 100644 --- a/README.md +++ b/README.md @@ -24,15 +24,18 @@ 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) +1. Briefly compare and contrast `.forEach` & `.map` (2-3 sentences max) .forEach is to change an array. .map is a new changed array. -2. Explain the difference between a callback and a higher order function. - -3. What is closure? +2. Explain the difference between a callback and a higher order function. A higher-order function is a function that takes another function(s) as an argument(s) and/or returns a function to its callers. A callback function is a function that is passed to another function with the expectation that the other function will call it +3. What is closure? A closure is a feature in JavaScript where an inner function has access to the outer (enclosing) function's variables. 4. Describe the four rules of the 'this' keyword. + 1. When a function is called in the global scope, the this reference is by default bound to the global object (window in the browser, or global in Node.js). + 2. When a function is called with a context object, the this reference will be bound to this object. + 3. .call, .apply and .bind can all be used at the call site to explicitly bind this. + 4. JavaScript determines the value of this at runtime, based on the current context. So this can sometimes point to something other than what you expect. -5. Why do we need super() in an extended class? +5. Why do we need super() in an extended class? he super keyword in JavaScript is used in order to call the methods of the parent class. By itself, super() is used within a constructor function to call the parent constructor function. ### Task 1 - Project Set up From de8805119f8d96e009ec5b5743b95138dbca1f62 Mon Sep 17 00:00:00 2001 From: User Date: Sun, 17 May 2020 11:31:34 -0500 Subject: [PATCH 6/7] Updates --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 99970f604a..a5a7bf7be4 100644 --- a/README.md +++ b/README.md @@ -30,12 +30,13 @@ Edit this document to include your answers after each question. Make sure to lea 3. What is closure? A closure is a feature in JavaScript where an inner function has access to the outer (enclosing) function's variables. 4. Describe the four rules of the 'this' keyword. - 1. When a function is called in the global scope, the this reference is by default bound to the global object (window in the browser, or global in Node.js). - 2. When a function is called with a context object, the this reference will be bound to this object. - 3. .call, .apply and .bind can all be used at the call site to explicitly bind this. - 4. JavaScript determines the value of this at runtime, based on the current context. So this can sometimes point to something other than what you expect. + 1. Whenever a function is contained in the global scope, the value of this inside of that function will be the window object. + 2. Whenever a function is called by a preceding dot, the object before that dot is this. + 3. Whenever JavaScript’s call or apply method is used, this is defined. -5. Why do we need super() in an extended class? he super keyword in JavaScript is used in order to call the methods of the parent class. By itself, super() is used within a constructor function to call the parent constructor function. + 4. Whenever a constructor function is used, this refers to the specific instance of the object that is created and returned by the constructor function. + +5. Why do we need super() in an extended class? The super keyword in JavaScript is used in order to call the methods of the parent class. By itself, super() is used within a constructor function to call the parent constructor function. ### Task 1 - Project Set up From 98296eec7586378366b499cbffa434dad64885b8 Mon Sep 17 00:00:00 2001 From: User Date: Mon, 18 May 2020 21:02:46 -0500 Subject: [PATCH 7/7] Progress --- challenges/arrays-callbacks.js | 13 ++++++++----- challenges/prototypes.js | 10 +++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 16ad4f5200..bfa4166617 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -1,7 +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. +// const zooAnimals = [ { animal_name: "Jackal, asiatic", population: 5, scientific_name: "Canis aureus", state: "Kentucky" }, @@ -35,8 +35,7 @@ pattern: "jackal, asiatic". Log the resut. */ -const lowCaseAnimalNames = zooAnimals.map(animal_name) { -} +const lowCaseAnimalNames = zooAnimals.map(animal_name) return {"animal_name": animal.lowCaseAnimalNames} console.log(lowCaseAnimalNames); @@ -81,8 +80,12 @@ 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 consume(cb, b, a) +return cb (a, b) + function add (cb) -return sum (a+b); +return (a+b) function multiply (cb) return a*b; @@ -90,7 +93,7 @@ return a*b; function greeting (cb) return 'Hello ${first-name} ${last-name} , nice to meet you' -/* Step 3: Check your work by un-commenting the following calls to consume(): */ +//* 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! diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 22597c4d86..59bc4c161a 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -9,7 +9,7 @@ function CuboidMaker(length, width, height) { this.length = length; this.width = width; this.height = height; - +} /* == Step 2: Volume Method == @@ -18,7 +18,7 @@ function CuboidMaker(length, width, height) { Formula for cuboid volume: length * width * height */ -method1.prototype.volume = length * width * height +CuboidMaker.prototype.volume = length * width * height /* == Step 3: Surface Area Method == Create another method using CuboidMaker's prototype that returns the surface area of a given cuboid's @@ -26,13 +26,13 @@ method1.prototype.volume = length * width * height Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height) */ -method2.prototype.surfaceArea = 2 * (length * width + length * height + width * height) +CuboidMaker.prototype.surfaceArea = 2 * (length * width + length * height + width * 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 CuboidMaker = new Object(); // 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 }