From 6fc74034aa33eeaa7e72b6117d6852e379c2aca1 Mon Sep 17 00:00:00 2001 From: Austin Littlefield Date: Sun, 16 Aug 2020 18:13:36 -0700 Subject: [PATCH 01/13] push --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index cc6d6902fb..c45adaff29 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,9 @@ 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) +The .forEach array method is used like the for loop that we have used before. The .map function works in the same way, but returns a new array + + 2. Explain the difference between a callback and a higher order function. 3. What is closure? From 5ed819729e922a30f56dcf84874b50287e2e1427 Mon Sep 17 00:00:00 2001 From: Austin Littlefield Date: Sun, 16 Aug 2020 18:33:26 -0700 Subject: [PATCH 02/13] task1arrays --- challenges/arrays-callbacks.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 12af878ceb..7948a0e34d 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -20,7 +20,15 @@ const zooAnimals = [ 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 = []; +zooAnimals.forEach((item) =>{ + displayNames.push(`Name: ${item.animal_name}`); + + displayNames.push(`Scientific: ${item.scientific_name}`); +}); + + console.log(displayNames); /* Request 2: .map() From 6afa1fb62b5a775790fc3d2915cfb900a39506e7 Mon Sep 17 00:00:00 2001 From: Austin Littlefield Date: Sun, 16 Aug 2020 18:40:17 -0700 Subject: [PATCH 03/13] task2arrays --- challenges/arrays-callbacks.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 7948a0e34d..579baa0694 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -38,6 +38,10 @@ The zoos need a list of all their animal's names (animal_name only) converted to */ const lowCaseAnimalNames = []; +zooAnimals.map((item) =>{ + lowCaseAnimalNames.push(`${item.animal_name.toLowerCase()}`); +}); + console.log(lowCaseAnimalNames); /* Request 3: .filter() From 599cd97eacc08b2fe8c409d6cb589e8541828e7d Mon Sep 17 00:00:00 2001 From: Austin Littlefield Date: Sun, 16 Aug 2020 18:53:28 -0700 Subject: [PATCH 04/13] task3arrays --- challenges/arrays-callbacks.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 579baa0694..18a0daf98b 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -50,8 +50,16 @@ The zoos are concerned about animals with a lower population count. Using filter */ const lowPopulationAnimals = []; +zooAnimals.filter((item) =>{ + lowPopulationAnimals.push(item.population < 5); + return item + +}); console.log(lowPopulationAnimals); +//review + + /* 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. From d8fb58a57ab5ac4334b34566c78b3f8162c9862e Mon Sep 17 00:00:00 2001 From: Austin Littlefield Date: Sun, 16 Aug 2020 19:08:20 -0700 Subject: [PATCH 05/13] task4arrays --- challenges/arrays-callbacks.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 18a0daf98b..be4a70c1ca 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -66,6 +66,19 @@ The zoos need to know their total animal population across the United States. Fi */ let populationTotal = 0; +let zooArray = []; + +zooAnimals.map((item) =>{ + zooArray.push(item.population); +}) + + +zooArray.reduce((cb,index) => { + populationTotal = cb + index; + return populationTotal +}, 0); + + console.log(populationTotal); From a9eea358f4b5466db2feb358c73ef955488beceb Mon Sep 17 00:00:00 2001 From: Austin Littlefield Date: Sun, 16 Aug 2020 19:24:23 -0700 Subject: [PATCH 06/13] task1closure --- challenges/arrays-callbacks.js | 23 ++++++++++++++++++++--- challenges/closure.js | 12 +++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index be4a70c1ca..81f9ca3b23 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -91,6 +91,11 @@ console.log(populationTotal); * 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 @@ -98,11 +103,23 @@ 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){ + return a + b +} + +function multiply(a,b){ + return a * b +} + +function greeting(a,b){ + 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..81a542ae7a 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -17,8 +17,18 @@ function myFunction() { myFunction(); // Explanation: - +// Because the nestedFunction is able to use variables that are set on a higher order function. Functions can call upon variables in higher order functions, but can not call upon variables nested lower. If this variable was in the global scope it could also be called upon. /* 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(parameter){ +let counter = 0; +for(let i = 0; i <= parameter; i++){ + counter += i; +} +return counter; +} +console.log(sumation(4)); \ No newline at end of file From c80d1b05999242e11f56b0302bb45772b113354a Mon Sep 17 00:00:00 2001 From: Austin Littlefield Date: Sun, 16 Aug 2020 19:30:19 -0700 Subject: [PATCH 07/13] task1 --- challenges/prototypes.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..8548abcea9 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -6,6 +6,12 @@ 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 @@ -13,6 +19,9 @@ 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. From 13cb3210cf7269fdc87874eb19e42aa8492cdcc6 Mon Sep 17 00:00:00 2001 From: Austin Littlefield Date: Sun, 16 Aug 2020 19:31:52 -0700 Subject: [PATCH 08/13] task3 --- challenges/prototypes.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 8548abcea9..c7c8e87432 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -29,6 +29,12 @@ CuboidMaker.prototype.volume = function () { Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height) */ +CuboidMaker.prototype.volume = 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 From a03c4bdcc475f0b2c6e662ee50d65bdeaf5e8465 Mon Sep 17 00:00:00 2001 From: Austin Littlefield Date: Sun, 16 Aug 2020 19:39:38 -0700 Subject: [PATCH 09/13] closurefinal --- challenges/prototypes.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/challenges/prototypes.js b/challenges/prototypes.js index c7c8e87432..d7c2269ca8 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -29,7 +29,7 @@ CuboidMaker.prototype.volume = function () { Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height) */ -CuboidMaker.prototype.volume = function () { +CuboidMaker.prototype.surfaceArea = function () { return 2 * (this.length * this.width + this.length * this.height + this.width * this.height) } @@ -41,8 +41,10 @@ CuboidMaker.prototype.volume = function () { 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 f897b3c90e515737422858e0d4a767efe859df90 Mon Sep 17 00:00:00 2001 From: Austin Littlefield Date: Sun, 16 Aug 2020 19:43:17 -0700 Subject: [PATCH 10/13] classes --- challenges/classes.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..9a4d31e4a4 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,28 @@ // 1. Copy and paste your prototype in here and refactor into class syntax. +class CuboidMaker{ +constructor(length,width,height){ + this.length = length; + this.width = width; + this.height = 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 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 // 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 2e2b46f500f93cf7d8532580a52b6f7d5c3573f7 Mon Sep 17 00:00:00 2001 From: Austin Littlefield Date: Sun, 16 Aug 2020 19:54:53 -0700 Subject: [PATCH 11/13] arrayrevised --- challenges/arrays-callbacks.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 81f9ca3b23..8fe901b11c 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -51,14 +51,13 @@ The zoos are concerned about animals with a lower population count. Using filter */ const lowPopulationAnimals = []; zooAnimals.filter((item) =>{ - lowPopulationAnimals.push(item.population < 5); - return item + if(item.population < 5){ +return lowPopulationAnimals.push(item.animal_name); +} }); console.log(lowPopulationAnimals); -//review - /* Request 4: .reduce() From 6f9229a3433deb5893c440e57a728e54720e13b9 Mon Sep 17 00:00:00 2001 From: Austin Littlefield Date: Sun, 16 Aug 2020 20:07:58 -0700 Subject: [PATCH 12/13] answers --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c45adaff29..8c4bd60c2b 100644 --- a/README.md +++ b/README.md @@ -26,16 +26,25 @@ 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) -The .forEach array method is used like the for loop that we have used before. The .map function works in the same way, but returns a new array +The .forEach array method is used the same as a for loop and iterates through all the items in an array. The .map function works in the same way, but returns a new array of the items that were requested. 2. Explain the difference between a callback and a higher order function. +A higher order function takes another function as an argument and returns that function to its callers. A callback function is a function that is passed to another function for that function to then call. + 3. What is closure? +It is the combination of a function and the reference to its surrounding state, known as the lexical environment. 4. Describe the four rules of the 'this' keyword. +The first principle is Window/Global Binding. It returns the "this" keyword to the global object and not the one in a declared object. It references the global scope. +Implicit Binding is when a function is contained within an object and that object is then referenced by "this". Such as russian nesting dolls inside of each other. + The 'new' Binding is defined by an instance of an object being created using the new keyword. It can then be printed out like a factory. + The Explicit Binding (call/apply/bind) applies to instances when a function needs to be called explicitly. Apply and call are used to pass parameters to the function. Explicit binding sets the value you want to "this". 5. Why do we need super() in an extended class? +Because it is used to access all of the parents properties and methods on the constructor. + ### Task 1 - Project Set up From e5ea61d2f26796a77515aabf7ea71654bbea169c Mon Sep 17 00:00:00 2001 From: Austin Littlefield Date: Sun, 16 Aug 2020 20:25:34 -0700 Subject: [PATCH 13/13] stretch --- challenges/classes.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/challenges/classes.js b/challenges/classes.js index 9a4d31e4a4..6e7a883b87 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -16,13 +16,34 @@ surfaceArea() { return 2 * (this.length * this.width + this.length * this.height + this.width * this.height) } + } +class CubeMaker extends CuboidMaker{ + constructor(length,width,height){ + super(length,width,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 cuboid = new CuboidMaker(4,5,5) + const cuboid1 = new CubeMaker(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(cuboid1.volume()); // 100 +console.log(cuboid1.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