From b45daa3af38b9c0983245af12ff2b35ce37fed32 Mon Sep 17 00:00:00 2001 From: noah niesen Date: Fri, 15 May 2020 18:58:07 -0500 Subject: [PATCH 1/4] finished self-study questions and arrays-callbacks tasks --- README.md | 15 +++++++++++++++ challenges/arrays-callbacks.js | 35 ++++++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index cc6d6902fb..9b96469bf8 100644 --- a/README.md +++ b/README.md @@ -26,14 +26,29 @@ 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) +.map() targets elements in an array and creates a new array for the targeted elements, which can be manipulated in numerous ways. +.forEach() simply targets elements in an array which can then be manipulated in numerous ways, without creating a new array. + 2. Explain the difference between a callback and a higher order function. +A higher-order function is a function that takes another function as an argument. +A callback function is a function typically found within higher-order functions. + 3. What is closure? +Code that is only available within its own environment/braces and outside of its environment/braces. + 4. Describe the four rules of the 'this' keyword. +Is the function called by new? +Is the function called by call(), apply(), or bind()? +Is the function called as a method? example: object.function()? +Is the function called in the global scope? + 5. Why do we need super() in an extended class? +If used in a constructor function in a child function, it transfers all of the properties of its parent. + ### 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 472ab3e96d..b09637ab42 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -21,6 +21,9 @@ The zoos want to display both the scientific name and the animal name in front o */ const displayNames = []; + +zooAnimals.forEach(animal => displayNames.push(`Name: ${animal.animal_name} Scientific: ${animal.scientific_name}.`)); + console.log(displayNames); /* Request 2: .map() @@ -29,7 +32,8 @@ The zoos need a list of all their animal's names (animal_name only) converted to */ -const lowCaseAnimalNames = []; +const lowCaseAnimalNames = zooAnimals.map(animal => animal.animal_name.toLowerCase()); + console.log(lowCaseAnimalNames); /* Request 3: .filter() @@ -37,7 +41,8 @@ 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(animal => animal.population < 5); + console.log(lowPopulationAnimals); /* Request 4: .reduce() @@ -45,7 +50,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((animalPopulation, animal) => { + return animalPopulation += animal.population; +}, 0); console.log(populationTotal); @@ -58,6 +65,10 @@ 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,14 +76,22 @@ 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(num1, num2) { + return num1 + num2; +} -/* 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! - +function multiply(num1, num2) { + return num1 * num2; +} +function greeting(firstName, lastName) { + return `Hello ${firstName} ${lastName}, 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! /* From ff94df0a03453d9637059371438a90727255239d Mon Sep 17 00:00:00 2001 From: noah niesen Date: Fri, 15 May 2020 19:09:52 -0500 Subject: [PATCH 2/4] completed closure.js --- challenges/closure.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/challenges/closure.js b/challenges/closure.js index 101d68e553..324e54144e 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -18,7 +18,19 @@ myFunction(); // Explanation: +// nestedFunction() lays within the closure of myFunction(), so nestedFunction() can access anything within myFunction(), but myFunction() wouldn't be able to access the contents of nestedFunction(). /* 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(num) { + let result = 0; + for (var i = 1; i <= num; i++) { + result += i; + } + return result; +} + +let output = sumation(4); +// console.log(output); \ No newline at end of file From 7d4fdf75d229846bc573472aec6ad903dc812bdc Mon Sep 17 00:00:00 2001 From: noah niesen Date: Fri, 15 May 2020 19:28:53 -0500 Subject: [PATCH 3/4] completed prototypes.js --- challenges/prototypes.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..bd66c38ddc 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -7,12 +7,23 @@ */ +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 == Create another method using CuboidMaker's prototype that returns the surface area of a given cuboid's length, width, and height. @@ -20,14 +31,17 @@ 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. */ -// Test your volume and surfaceArea methods by uncommenting the logs below: -// console.log(cuboid.volume()); // 100 -// console.log(cuboid.surfaceArea()); // 130 - +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 From b0148b3947eb229d5af7f3878d524bfd85253e78 Mon Sep 17 00:00:00 2001 From: noah niesen Date: Fri, 15 May 2020 19:42:15 -0500 Subject: [PATCH 4/4] completed classes.js --- challenges/classes.js | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..525b7b9461 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,42 @@ // 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 -// 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. + +class CubeMaker extends CuboidMaker { + constructor(length, width, height) { + super(length, width, height); + } + + volume() { + return this.width ** 3; + } + + surfaceArea() { + return 6 * (this.width ** 2); + } +} + +const cube = new CubeMaker(3, 3, 3); +// console.log(cube.volume()); +// console.log(cube.surfaceArea()); \ No newline at end of file