diff --git a/README.md b/README.md index cc6d6902fb..8c4bd60c2b 100644 --- a/README.md +++ b/README.md @@ -26,13 +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 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 diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 12af878ceb..8fe901b11c 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() @@ -30,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() @@ -38,14 +50,34 @@ The zoos are concerned about animals with a lower population count. Using filter */ const lowPopulationAnimals = []; +zooAnimals.filter((item) =>{ + if(item.population < 5){ +return lowPopulationAnimals.push(item.animal_name); +} + +}); 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. */ 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); @@ -58,6 +90,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 @@ -65,11 +102,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/classes.js b/challenges/classes.js index 992e39dc0b..6e7a883b87 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,49 @@ // 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) + + } + + +} + +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(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 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 diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..d7c2269ca8 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. @@ -20,14 +29,22 @@ 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. */ +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